Public Group

learning the proper way to loop...

This topic is 4930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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";
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
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
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
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
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
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
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
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
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
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

• 16
• 9
• 13
• 41
• 15