Hey, I'm relatively new to C++, started a few days ago and I'm new to this forum community as well. I made a simple program where the computer asks you for your name, then age, followed by your hometown and your occupation (in that order). It works fine, but there's one small problem. If I were to type my name in as "Bob Ray", it will read my name as "Bob" and my age as "Ray". I would like to fix that little problem. Here's my code...

#include <iostream>
#include <string>

using namespace std;

void Convo ();

string name, city, job, age;
void Convo ()
{
     cout << "Hello! What's your name?" << endl;    
     cin >> name;
     cout << endl;
     cout << "Hi there, " << name << "." << endl;
     cout << endl;
     cout << "How old are you, " << name << "?" << endl;
     cin >> age;
     cout << endl;
     cout << "Wow! " << age << " years old. You're ancient!" << endl;
     cout << endl;
     cout << "Where are you from?" <<endl;
     cin >> city;
     cout << endl;
     cout << "Sounds like a fun place." << endl;
     cout << endl;
     cout << "What's your profession?" << endl;
     cin >> job;
     cout << endl;
     cout << "Wow! You're a " << job << "!" << endl;
     cout << endl;
     cout << "Well that's all the questions I have for you." << endl;
     cout << endl;
     Convo();
}


int
main()
{
      Convo();
      return 0;
}

I'm using Bloodshed Dev C++ (if that matters). Also, if you could tell me how to allow the user to exit the program by hitting the 'Esc' key, I'd be entirely grateful.


Thanks for the help,

Trinimini

Recommended Answers

All 3 Replies

Welcome to DaniWeb

You need to look up the getline( ) method. cin >> will only read in one "word" worth at a time - it skips whitespace, grabs displayable characters, stops when it again encounters whitespace.

Also, please use the code tags to make your code display better (spacing/indenting maintained)

[code]

your code goes here

[/code]

Instead of cin>>name you could use getline(cin, name); to get the first name and the surname :) ...

commented: It took you 5 hours to come up what had been already said ! -1

Thank you kindly everyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.