How To Ask Question, Get Input, Clear Screen, Ask Question, Get Input, Clear Screen.....

Hello All.  I am a newbie to C++ programming.  As a matter of fact, I just read a few tutorials that really peaked my interest in C++.  I am so excited to start that I wanted to create this simple program:

In a console window (dos) I would like to ask the user a question and save the response as a variable.  Then I would like the screen to clear and repeat the process.  The only problem is that when the screen clears, it does not ask the other questions.  It just says "press any key to continue" and then closes the window.  Here is my code:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  int a, b, c, d;
 
  cout<<"What is your name?\n\n";
  cin>>a;
  system("cls");
 
  cout<<"How old are you?\n\n";
  cin>>b;
  system("cls");
 
  cout<<"Who is your employer?\n\n";
  cin>>c;
  system("cls");
 
  cout<<"What is your favorite website?\n\n";
  cin>>d;
  system("cls");
 
  system("PAUSE");      
  return 0;
}

This should be fairly easy for you pros out there. Is there something wrong with my code?  Obviously there is.  Can someone please help me?

Thanks,
Scott
cscottyAsked:
Who is Participating?
 
AxterConnect With a Mentor Commented:
Hi cscotty,
> >  cout<<"What is your name?\n\n";
> >  cin>>a;

In above code, you're asknig for a name, which is not a number, however you're input type is an integer type, so your cin is expecting a number.
This can through the stream off.


David Maisonave :-)
Cheers!
0
 
AxterCommented:
cscotty,

Once you make sure your input types match expected input, that should fix the problem

David Maisonave :-}
0
 
cscottyAuthor Commented:
Oh My Word, AXTER You are a GENIUS!!!!!!!!!!!!!

I changed the type to "char" instead of "int" and it worked!!!!!!!  Brilliant!!!!  I am so overwhelmed right now, I gotta catch my breath.  I've been searching for the answer for over a month now and I knew I shouldve come here first.  Thanks a billion AXTER.  Truly a genius.  Here is my new code that WORKS:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  char a, b, c, d;
 
  cout<<"What is your name?\n\n";
  cin>>a;
  system("cls");
 
  cout<<"How old are you?\n\n";
  cin>>b;
  system("cls");
 
  cout<<"Who is your employer?\n\n";
  cin>>c;
  system("cls");
 
  cout<<"What is your favorite website?\n\n";
  cin>>d;
  system("cls");
 
  system("PAUSE");      
  return 0;
}

Thanks a BunCH!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.