Jump to content

C++ While Loop Problem...


Αντώνης
 Share

Recommended Posts

With that i know, in theory it must work....

 

i cant find problem ... but it dosent work, my program end even size is  > 4

 

# include <iostream>

using namespace std;

void main() {
  
  int Year;
  int xSize;

  cin >> Year; // Example we put 2011 
  xSize = sizeof(Year); // Size of Year / Size of 2011 is 4

  // While size is > 4 then loop ...
  while (xSize > 4){
 
           cout << "GTFO Man, limit is 4 numbers.... Again Year...";
           cin >> Year;
 }

}

 

 

PS: Dont tell me use <1 & >9999, i dont want this way.

Link to comment
Share on other sites

if you put, for eg 12212 at year.. it will show you that the size is 4 :|

 

my english is not so good, anyway that i want is limit of numbers max 4 numbers as year, dont want put year example 12345

 

BTW im using wrong way i think, sizeof(_val); i think means the size of bytes....

 

 

ahaha :(... again from 0 ... loking for answer...How can. ...

Link to comment
Share on other sites

I told you... either this way:

if((year>=0)&&(year>=9999))
{
   //code
}
else
   cout <<"GTFO!"endl;

 

or my other post ;)

here:

 

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
bool succ;
int intYear;
string strYear;
cout <<"Value for strYear:";
cin >> strYear;
if(strYear.length()<=4)
{
	stringstream sti(strYear);
	sti >> 	intYear;   //this should be two lines lower but there's a problem with the if...nvm it works like this too
	if(intYear==0)
	{
		cout<<"Wrong year input...use numbers!"<<endl;
		succ=0;
	}
	else
	{
		cout<<"strYear is <= 4 digits, converting it to integer in variable intYear..."<<endl;
		succ=1;
	}
}
else
{
	cout<<"Wrong year input.(must be < 9999)"<<endl;
	succ=0;
}

if(succ==1)
{
	//code
}
else
{
	cout <<"exiting program..."<<endl;
}
return 0;
}

Link to comment
Share on other sites

Hax0r i know those 2 ways...

i dont want using <1 >9999

 

i dont want create a string and put value of int on it... using lenght()

 

just with 1 way ;ppp

... anyway bcause professor say who find it wil take +1 bonus

 

his question was simple

 

how can print only 4 numbers using cout

 

exmaple if we put 123456789 cout wil print only 1234

 

well i hope i find it before 18:00 and i take bonus xD

 

and if not np ...

 

my english  sux ;p

Link to comment
Share on other sites

Did the professor give you the right answer?

meanwhile I thought of this :P

 

#include <iostream>
using namespace std;

int main()
{
int year;
do{
	cin >>year; //has to be >0
}while(year-9999>=1);
        //code
return 0;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock