Page 1 of 1

What command do you use, to go to another variable if...?

Posted: Sat Jan 10, 2009 4:23 pm
by stykat
I am still learning C++, and i am trying to make a program, that would look like this:
( im going to write it, rather like a pseudo code, not with the whole commands)

int number ;
cout<< " Please type in a number between 1 and 20 " ;
cin>>number ;
if ( number < 1 && number > 20 )
do
cout<< " Error, please type a number between 1 and 20 only " ;
and start the variable again.
Or, is, the number is smaller than 1 and bigger than 20, then repeat the variable instruction ( write cout<< please type in a number.... "

i fought the goto command will help but, it wont sicne i wouldv need to label that variable, and i don't really know whats that.

But then, i could write
if ( number is smaler than 1 and greater than 20)
cout<<"then cout please type a number between 1 and 20 " ;
cin>> number ;

Could, you (one of you programers) help me solving this mystery?

Thank you for reading the topic.

Re: What command do you use, to go to another variable if...?

Posted: Sat Jan 10, 2009 4:39 pm
by programmerinprogress
you could write it like this...

Code: Select all


int number = 0; 

do 
{
cout << "please type a number between 1 and 20"; 
cin >> number ;


}while((number < 1) || (number > 20) );

// then you insert your other code here 

i'll explain the thought behind it.

you use the Do...While statement to execute your block of code once (without it being evaluated, thus displaying the message), then your user enters the number, if they get it wrong, the compiler will jump back up to Do again, and repeat that block of code, however, if the number typed is correctly, then the block finishes, and then you can write the rest of your code!

hope this helped ;)

PS: you mentioned GoTo, I wouldn't use that, it's a form of unconditional branch (meaning it doesn't test for anything), and it can lead to hard to read (spaghetti) code, also there are better ways of doing most things in a High-level language, but if you keep at learning the language, i'm sure you'll pick up those little techniques that make C and C++ programming that bit sweeter than any other language!

SOME EXTRA STUFF: The first iteration (that is, the first time you execute) of Do..while works like an unconditional loop, so this code will always be executed once if the compiler comes across it, this allows you to do checks within the loop, increment variables, and many other things at least once before then compiler passes the loop, you can then do checks on any iterations after the first one, in this case I thought it was better to do it this way than using IF or GOTO.

Sorry if i'm being a little too elaborate, I just love to program :lol:

Re: What command do you use, to go to another variable if...?

Posted: Sat Jan 10, 2009 5:17 pm
by dandymcgee
On a side note:

Code: Select all

if ( number < 1 && number > 20 )
Means if number is less than one AND greater than twenty, which will NEVER be true.

Code: Select all

if ( number < 1 || number > 20 )
This is how you would check if number is less than one OR greater than twenty.

Re: What command do you use, to go to another variable if...?

Posted: Sat Jan 10, 2009 5:33 pm
by programmerinprogress
Yeah that's completely right, what was I thinking? :lol:

(I edited my original post so that the logic is correct now)

Re: What command do you use, to go to another variable if...?

Posted: Sat Jan 10, 2009 6:30 pm
by PixelP
dandymcgee wrote:On a side note:

Code: Select all

if ( number < 1 && number > 20 )
Means if number is less than one AND greater than twenty, which will NEVER be true.

Code: Select all

if ( number < 1 || number > 20 )
This is how you would check if number is less than one OR greater than twenty.
one good old mistake :lol:

Re: What command do you use, to go to another variable if...?

Posted: Sun Jan 11, 2009 3:47 pm
by stykat
programmerinprogress wrote:you could write it like this...

Code: Select all


int number = 0; 

do 
{
cout << "please type a number between 1 and 20"; 
cin >> number ;


}while((number < 1) || (number > 20) );

// then you insert your other code here 

i'll explain the thought behind it.

you use the Do...While statement to execute your block of code once (without it being evaluated, thus displaying the message), then your user enters the number, if they get it wrong, the compiler will jump back up to Do again, and repeat that block of code, however, if the number typed is correctly, then the block finishes, and then you can write the rest of your code!

hope this helped ;)

PS: you mentioned GoTo, I wouldn't use that, it's a form of unconditional branch (meaning it doesn't test for anything), and it can lead to hard to read (spaghetti) code, also there are better ways of doing most things in a High-level language, but if you keep at learning the language, i'm sure you'll pick up those little techniques that make C and C++ programming that bit sweeter than any other language!

SOME EXTRA STUFF: The first iteration (that is, the first time you execute) of Do..while works like an unconditional loop, so this code will always be executed once if the compiler comes across it, this allows you to do checks within the loop, increment variables, and many other things at least once before then compiler passes the loop, you can then do checks on any iterations after the first one, in this case I thought it was better to do it this way than using IF or GOTO.

Sorry if i'm being a little too elaborate, I just love to program :lol:

THANK YOU SOO MUCH FOR YOUR HELP

SO like youre right.
I did a loop, as long as, you type any other number thats not between 1 and 20, it will change.
One of the problems was : expected primary-expression before "else" .

But then i made the first a loop. and the rest as it is first condition starts with if, then the others, else if .

This is what i made so far :

Code: Select all


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 int gorex ;
 cout<< " Please type in a number bewtween 1 and 20 : "<<endl ;
 cin>> gorex ;
 
 while (gorex == 21 || gorex > 21 || gorex < 1 )  
    {
 cout<< " Error, please type in a number between 1 and 20 "<<endl;
 cin>> gorex ;  
       }
  if   (gorex > 1 || gorex < 10 ) 
      {
  cout<< " This number is smaler than 20 "<<endl ;
         }
 else  if  (gorex > 10 || gorex < 20 )  
       {
 cout<< " The number is bigger than 10 but smaller than 20 "<<endl ;
        }
 
 else if (gorex == 10 ) {
 cout<< " The number is 10 "<<endl ;
        }
 else if (gorex == 20 ) {
 cout<< " The number is 20, the varialbe is at full size "<<endl ;
        }
 cin.ignore() ; 

 
    system("PAUSE");
    return EXIT_SUCCESS;
}


I will want to, make an error in case, the user will not type a number .
i thought of

Code: Select all

while ( gorex == string ) {
cout<< " Please INPUT only numbers " ;
cin>> gorex ;
still, it won't work since, there is a thing expected before ' ) '.

Thank you again for helping me resolve this mistery .

All the best.

Re: What command do you use, to go to another variable if...?

Posted: Sun Jan 11, 2009 5:27 pm
by KuramaYoko10
Hey dude, the solution is simpler than that I guess...


Instead of trying to compare an int to a char or string, you just have to add an else at the end of everything, that way it will output the error message for any characters that does not fit the conditions before.
Like this:

Code: Select all

if(gorex > 1 && gorex < 10)
	cout <<"Your number is between 2 and 10" <<endl;

else if(gorex == 10)
	cout <<"Your number is exactly 10" <<endl;

else if(gorex > 10 && gorex < 20)
	cout <<"Your number is between 10 and 20" <<endl;

else if(gorex == 20)
	cout <<"Your number is exactly 20" <<endl;

else
	cout <<"Please input ONLY numbers between 1 and 20" <<endl;

Re: What command do you use, to go to another variable if...?

Posted: Mon Jan 12, 2009 1:48 am
by stykat
WEll, i tried with that, but, it didn't worked.
If i typed a number that did not fit the condition it told me : the number is smaller than 20.

Re: What command do you use, to go to another variable if...?

Posted: Mon Jan 12, 2009 4:47 am
by programmerinprogress
I looked at your code and the code kuramayoko10 posted, and I can see the problem.

when your doing logic like this, it's preferable to account for every possible scenario, the reason why the value was seen as being less than 20 was probably because of the order you tested the logic.

It is very important you go from Big to Small, as certain conditions have the potential to clash, so you want your code to test everything in order, so that the program can break away at the correct time without any unwanted side effects.

NOTE: I used do...while again, what can I say, I like it!

Code: Select all

#include<iostream>
using namespace std;

int main(int argc,char * argv[])
{
    int gorex(0); // keeping this for consitency, although I don't get the name

    do
    {
        cout << "Please Type a number between 1 and 20" << endl;
        cin >> gorex;
        if(gorex > 20)
        {
            cout << "Number is more than 20, Please Try Again!" << endl;
        }
        else if(gorex == 20)
        {
            cout << "Number is 20" << endl;
        }
        else if(gorex < 20 && gorex >10)
        {
            cout << "Number is less than 20" << endl;
        }
        else if(gorex == 10)
        {
            cout << "Number is equal to 10" << endl ;
        }
        else if(gorex < 10 && gorex > 1)
        {
            cout << "Number is less than 10" << endl ;
        }
        else if(gorex == 1)
        {
            cout << "Number is equal to 1" << endl ;
        }
        else
        {
            cout << "Number is less than 1, Please Try Again" << endl;
        }

    }while(gorex < 1 || gorex > 20);

    return 0;
}

I tested this code in my own IDE and it seemed to work, take note of the combination of && and || logic used throughout the program.

I'm not trying to be critical skykat, but I found your code quite hard to read, clean use of whitespace and indentation goes a long way.

Re: What command do you use, to go to another variable if...?

Posted: Mon Jan 12, 2009 5:09 am
by KuramaYoko10
Ah... I see, I did real quick now this little program so you can compare with yours!!

I compiled and it works with only one thing happening... if the user types a char it will keep looping through the phrase "You have typed an invalid number" ... I dont have time now to check on that but I will try fixing that later!
For now you could just add a 'return 0' there or 'break' that it will close the screen instead of looping it!!

Here is the code:

Code: Select all

#include <iostream>

using namespace std;

int gorex;

int main()
{
	while(true)
	{
		system("CLS");

		cout <<"Please type a number between 1 and 20:" <<endl <<endl;
		cin >> gorex;
		system("CLS");


		if(gorex >= 1 && gorex <= 20)
		{
			if(gorex >= 1 && gorex < 10)
				cout <<"Your number is between 1 and 10!" <<endl <<endl;

			else if(gorex == 10)
				cout <<"Your number is exactly 10!" <<endl <<endl;

			else if(gorex > 10 && gorex < 20)
				cout <<"Your number is between 10 and 20!" <<endl <<endl;

			else if(gorex == 20)
				cout <<"Your number is exactly 20!" <<endl <<endl;

			else
				cout <<"Please input ONLY numbers between 1 and 20!" <<endl <<endl;
		}

		else
		{
			cout <<"You have typed an invalid number!!" <<endl <<endl;
         break;
		}
		

		system("PAUSE");
	}

	return 0;
}
Hope it works xD


EDIT:
Just now I have seen programmerinprogress response... thanks for making my code better xD

Re: What command do you use, to go to another variable if...?

Posted: Mon Jan 12, 2009 3:37 pm
by MarauderIIC
"while(true)" this will never exit since you don't have 'break' in your loop anywhere
Also the reason it loops forever if you put in a char is because your variable is an integer and cin is expecting an integer (cin >> gorex). If cin gets something that isn't an integer, it silently enters a 'fail state' and essentially no longer accepts input (it just get skipped over, in other words)

I think this'll fix it.

Code: Select all

int gorex;
cin >> gorex;
if (cin.fail()) {
    cout >> "That's not a number!" << endl;
    cin.clear();
}

Re: What command do you use, to go to another variable if...?

Posted: Mon Jan 12, 2009 3:58 pm
by stykat
I will just stay, at,, the, do a condition for everything, since, if i would try to make a loop for that error, it will just loop it forever, ok, i learned so far alot, so now i could get my eyes on the book again.
Thank you guys for the cooperation.

About the writing, well i got a bad hand writing too, i used to write worse than a doctor.
And i didn't tried to make a style for it, or to make it cleaner, but, i will learn making a code cleaner since if i would want to show it to someone, such as you guys, then, this would look like ketchup, with mustard, and American Dressing, on French Fries (like shit for some, but, i say it would taste good:} )

Re: What command do you use, to go to another variable if...?

Posted: Mon Jan 12, 2009 5:15 pm
by programmerinprogress

Code: Select all

int gorex;
cin >> gorex;
if (cin.fail()) {
    cout >> "That's not a number!" << endl;
    cin.clear();
}
This wont actually reset cin for me, I still can't do any input, are there any other ways of achieving a reset of cin?

Re: What command do you use, to go to another variable if...?

Posted: Mon Jan 12, 2009 7:58 pm
by MarauderIIC
Probably need to also cin.ignore(some big number or the input limit, '\n')
Sorry it's not something I do all that much :)

Re: What command do you use, to go to another variable if...?

Posted: Tue Jan 13, 2009 5:40 am
by KuramaYoko10
Hey Maurader thanks for the tip (I added a break on the 'else' of that code) ...

and AHA I was looking for the cin.clear() and cin.ignore(), when I was typing the post here in the forum I remembered slightly those commands but at that time I didnt have time to look for it on internet xD
now I am updated!!!


thanks dude !!