What command do you use, to go to another variable if...?
Moderator: Coders of Rage
What command do you use, to go to another variable if...?
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.
( 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.
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: What command do you use, to go to another variable if...?
you could write it like this...
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
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
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
Last edited by programmerinprogress on Sat Jan 10, 2009 8:44 pm, edited 3 times in total.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: What command do you use, to go to another variable if...?
On a side note:
Means if number is less than one AND greater than twenty, which will NEVER be true.
This is how you would check if number is less than one OR greater than twenty.
Code: Select all
if ( number < 1 && number > 20 )
Code: Select all
if ( number < 1 || number > 20 )
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: What command do you use, to go to another variable if...?
Yeah that's completely right, what was I thinking?
(I edited my original post so that the logic is correct now)
(I edited my original post so that the logic is correct now)
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
- PixelP
- Chaos Rift Regular
- Posts: 153
- Joined: Tue Oct 07, 2008 12:23 pm
- Programming Language of Choice: c/c++
- Location: sweden
- Contact:
Re: What command do you use, to go to another variable if...?
one good old mistakedandymcgee wrote:On a side note: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.Code: Select all
if ( number < 1 || number > 20 )
Re: What command do you use, to go to another variable if...?
programmerinprogress wrote:you could write it like this...i'll explain the thought behind it.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
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
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 ;
Thank you again for helping me resolve this mistery .
All the best.
- KuramaYoko10
- Chaos Rift Cool Newbie
- Posts: 55
- Joined: Fri Oct 31, 2008 8:02 pm
Re: What command do you use, to go to another variable if...?
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:
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;
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.
Re: What command do you use, to go to another variable if...?
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.
If i typed a number that did not fit the condition it told me : the number is smaller than 20.
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: What command do you use, to go to another variable if...?
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!
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.
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'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.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
- KuramaYoko10
- Chaos Rift Cool Newbie
- Posts: 55
- Joined: Fri Oct 31, 2008 8:02 pm
Re: What command do you use, to go to another variable if...?
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:
Hope it works xD
EDIT:
Just now I have seen programmerinprogress response... thanks for making my code better xD
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;
}
EDIT:
Just now I have seen programmerinprogress response... thanks for making my code better xD
Last edited by KuramaYoko10 on Tue Jan 13, 2009 5:37 am, edited 1 time in total.
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: What command do you use, to go to another variable if...?
"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.
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();
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: What command do you use, to go to another variable if...?
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:} )
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:} )
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: What command do you use, to go to another variable if...?
Code: Select all
int gorex;
cin >> gorex;
if (cin.fail()) {
cout >> "That's not a number!" << endl;
cin.clear();
}
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: What command do you use, to go to another variable if...?
Probably need to also cin.ignore(some big number or the input limit, '\n')
Sorry it's not something I do all that much :)
Sorry it's not something I do all that much :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- KuramaYoko10
- Chaos Rift Cool Newbie
- Posts: 55
- Joined: Fri Oct 31, 2008 8:02 pm
Re: What command do you use, to go to another variable if...?
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 !!
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 !!
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.