learning the proper way to loop...

Started by
19 comments, last by Woodsman 19 years, 5 months ago
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?
Grim_reaper7Lamp Geekz
Advertisement
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.
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)
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.
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?
Grim_reaper7Lamp Geekz
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?
Grim_reaper7Lamp Geekz
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.
-Vendal Thornheart=) Programming for a better tomorrow... well,for a better simulated tomorrow. ;)
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"
Grim_reaper7Lamp Geekz
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 . . .
If a plant cannot live according to its nature, it dies; so a man.
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.
If a plant cannot live according to its nature, it dies; so a man.
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

This topic is closed to new replies.

Advertisement