?
Solved

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

Posted on 2005-04-21
3
Medium Priority
?
228 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:cscotty
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 200 total points
ID: 13835835
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
 
LVL 30

Expert Comment

by:Axter
ID: 13835854
cscotty,

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

David Maisonave :-}
0
 

Author Comment

by:cscotty
ID: 13836459
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question