Sign in to follow this  
grim_reaper7

learning the proper way to loop...

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


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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: 10023123
What is your name?






tuomas
enter you favorite number tuomas
You are older than me by 10023102 years
Press any key to continue . . .


Share this post


Link to post
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 this post


Link to post
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 enter


getline (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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
//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 this post


Link to post
Share on other sites
Quote:
Original post by grim_reaper7
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
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 tuomas
You are older than me by 10023102 years
Press any key to continue . . .

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
it 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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by grim_reaper7
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
??
Have you read what I've been saying?
Quote:
Original post by Woodsman
The getline will read the string from the console up until a return/newline is encountered.

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this