# learning the proper way to loop...

## Recommended Posts

grim_reaper7    103
ok...looping is a lot diff. in c++ than php or vb, not understanding how i can make this:
//a small C++ program
#include <iostream>
#include <string>
using namespace std;

int main()
{
int cbare;
int alternate;
int older;
int younger;
string name;
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
cout<<"What is your name?\n";
cin>>cbare;
cin>>name;
while (name == ""){
cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!\n";
cin>>name;
}

cout<<"enter you favorite number "<<name<<"\n";

cin.ignore();

if (cbare > 21){
older = cbare - 21;
cout<<"You are older than me by "<<older<<" years\n";
}
else if (cbare < 21){
younger = 21 - cbare;
cout<<"I am older than you by "<<younger<<" yeas\n";
}else{
cout<<"You must be 21\n";
}
cin.get();
}


an unconinual loop that gives you a chance to put in your name if you haven't done so?

##### Share on other sites
SoulSkorpion    238
Er... what is it exactly that you're asking?

C's loops constructs are exactly the same as VB and PHP. It's got the same three kinds of loops: do-while, while-do and for.

##### Share on other sites
KrazeIke    256
Well, for one thing, you're reading an integer right after you ask for the name and then reading a string. If you get rid of that cin>>cbare line and the stuff below it that uses cbare, it works fine. Maybe you should read that integer after you ask for a number.

##### Share on other sites
grim_reaper7    103
grrr..the for, while, do yes are the same however; i want the user to have to type in his name before they can proceed, if you don't enter your name it's supposed to say "YOU must enter your name beotch" and give you another chance to enter your name..instead it just continuasly loops not giving you a chance to type...what am i missing?

##### Share on other sites
grim_reaper7    103
Quote:
 If you get rid of that cin>>cbare line and the stuff below it that uses cbare, it works fine.

it works...but when you don't put a name, doesn't comment say"you need to enter name beotch"...why is that?

##### Share on other sites
VThornheart    246
Dunno if this'll help, but try doing getline(cin, name); instead of cin >> name. It's better to do with string objects. I don't know if it'll solve your particular problem... from what it looks like, it should be doing the loop as you ask, it seems to me.

##### Share on other sites
grim_reaper7    103
Quote:
 but try doing getline(cin, name); instead of cin >> name.
tried but doesn't work...i don't get this one...? is there a different way to put a promted value into a string than an integer? "cin,name" as opposed to "cin>>name"

##### Share on other sites
Woodsman    426
Quote:
Original post by grim_reaper7
Quote:
 If you get rid of that cin>>cbare line and the stuff below it that uses cbare, it works fine.

it works...but when you don't put a name, doesn't comment say"you need to enter name beotch"...why is that?
Show us the exact console input/output.

It's working fine for me (IIRC, cin with string's will not return until something other than whitespace is entered) after I place the int above the string with a prompt (so the whole thing makes a little more sense.
Like:
...cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";  cout<<"Age: ";  cin>>cbare;  cout<<"What is your name?\n";    cin>>name;  while (name== ""){	  	  cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!\n";      cin>>name;  }...

HEY, you, I'm alive! Oh, and Hello World!Age: 10023123What is your name?        tuomasenter you favorite number tuomasYou are older than me by 10023102 yearsPress any key to continue . . .

##### Share on other sites
Woodsman    426
Quote:
Original post by grim_reaper7
Quote:
 but try doing getline(cin, name); instead of cin >> name.
tried but doesn't work...i don't get this one...? is there a different way to put a promted value into a string than an integer? "cin,name" as opposed to "cin>>name"
I'm positive of what you're asking. The getline will read the string from the console up until a return/newline is encountered. The cin >> someString will read until the first whitespace.

##### Share on other sites
kingpinzs    117
is this were the input is suppose to be blank if they did not put in there name?

while (name == ""){
cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!\n";
cin>>name;
}

it does not work that way but if your input == one space then it would do what you asked.

you need sommething like this
//This is used to accept no input as in put cin.ignore(); // you have to have this or it will see there is no input as input and continue any way with having to press entergetline (cin, input); while (input.empty()) //this you can just press enter to get what you want{    cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!\n";      cin>>name;}else

hope this is what you were asking

##### Share on other sites
grim_reaper7    103
see...actually it isn't working...when you don't input your name...it isn't saying "YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!" like it should...i get the same results you do! just no damn comment to sugest user to input name! point i am trying to get w/ this is making it more user friendly lol

##### Share on other sites
KrazeIke    256
//a small C++ program#include <iostream>#include <string>using namespace std;int main(){  string name;  cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";  cout<<"What is your name?\n";  getline(cin, name);  while (name == ""){	  cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!\n";      getline(cin, name);  }  cin.get();}

##### Share on other sites
Woodsman    426
Quote:
 Original post by grim_reaper7see...actually it isn't working...when you don't input your name...it isn't saying "YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!" like it should...i get the same results you do! just no damn comment to sugest user to input name! point i am trying to get w/ this is making it more user friendly lol
Oh. It's not actually reading anything. Did you see my output? cin >> someString skips all whitespace, so you hammering on the enter key won't do anything. The actual call returns after something is there to be read (non whitespace character(s)). Get it?

To illustrate:
  cout<<"What is your name?\n";    cin>>name;BECOMES:What is your name?                            // cin began here, you hit enter so it is still waiting                            // cin still here and next line and so on - you haven't entered the loop        tuomas              // but here it encounters 't' so it keeps reading until the ' ' character after 's'SO YOU NEVER GET HERE BECAUSE name == "tuomas"      while (name== ""){          cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!\n";         cin>>name;      }enter you favorite number tuomasYou are older than me by 10023102 yearsPress any key to continue . . .

##### Share on other sites
Alpha_ProgDes    6930
while (name == "\n") {....}

?
it seems that if a person hits Enter/Return on the keyboard then the newline character is produced. right?

##### Share on other sites
Woodsman    426
Quote:
 Original post by Alpha_ProgDesit seems that if a person hits Enter/Return on the keyboard then the newline character is produced. right?

I believe that I found the problem (which isn't a problem) above. I don't think that cin >> string::something will read whitespace (such as CR/LF).

##### Share on other sites
Zahlman    1682
The character is produced, but is gleefully ignored by the cin's operator>>(std::string). That operator simply won't read whitespace into the variable, and it will keep reading until there is some non-whitespace in the variable (and keep reading the non-whitespace until whitespace is seen again). '\n' is whitespace.

So to get the desired prompting behaviour, std::getline is required, yes.

##### Share on other sites
grim_reaper7    103
KrazeIke i copied and pasted ur's and it worked perfect...i am trying to figure what the rest of you sd so give me a sec...sorry all...i am really new, did this in a matter of 10-15min. w/ no experience in c++

##### Share on other sites
grim_reaper7    103
zahlman, that for some reason made perfect sense to me...so the simplest way prob. would be to compare the while to \n like Alpha_ProgDes suggested right?
Quote:
 while (name == "\n") {....}

##### Share on other sites
Guest Anonymous Poster
Hello world! err..yeah!

##### Share on other sites
grim_reaper7    103
lol...nope to my last comment...that doesn't work either, i get the have to getlne(cin, name) that makes sense, now my question is...why do i have to enter after i have typed my name to have it ask me my fav. number
??

##### Share on other sites
Woodsman    426
Quote:
 Original post by grim_reaper7lol...nope to my last comment...that doesn't work either, i get the have to getlne(cin, name) that makes sense, now my question is...why do i have to enter after i have typed my name to have it ask me my fav. number??
Have you read what I've been saying?
Quote:
 Original post by WoodsmanThe getline will read the string from the console up until a return/newline is encountered.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account