Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,132 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,801 people online right now. Registration is fast and FREE... Join Now!




not taking user input

 
Reply to this topicStart new topic

not taking user input

icanseethatimgoingblind
11 Apr, 2007 - 11:30 AM
Post #1

New D.I.C Head
*

Joined: 28 Mar, 2007
Posts: 2


My Contributions
[font=Arial]Hello.
I need to finish up my program for class. well i am trying to make a program that is kind of like a magic eight ball. i have it tell them to ask a question. it gives them an answer without taking input.
here is my code

CODE

//These two libraries must be used when you want random numbers.  
#include<iostream>
#include <stdlib.h>  
#include <time.h>
#include <string>
using namespace std;
bool play = true;
int choice;
char useless[200];
int useless2;
int useless3;
char again;
int main()

{    
while (play == true)
{    
cout<< "welcome to my Project you will either get your fortune or you stock advice" << endl;
cout<< "chooose one"  << endl;
cout<< "1. fourtune teller" << endl;
cout<< "2. stock advice" << endl;
cin>> choice;
if (choice == 1)
{
    cout << "hi and welcome to my fortune teller" << endl;// describing how its going to work
    cout<< "it works like a magic eight ball program" << endl;
     cout << "please enter in a question and it will tell you the answer.";
    cin.getline(useless, 200);
    cout << flush;
     srand((unsigned)time(NULL));
     int theNumber;
          
    
          
     theNumber = 1 + rand() % 4;
    if (theNumber == 1)
    {
     cout<<"No way."<<endl;                    
    }      
if (theNumber == 2)
{
     cout<<"Most likely, yes."<<endl;
}  
if (theNumber == 3)
{
     cout<<"Try again tomorrow."<<endl;
}  
if (theNumber == 4)
{
cout<<"Yes, I believe so!"<<endl;
}
cout << "Do you wish to start again? (Y/N)\n";//asking them if they want to start again.
                cin >> again;//storing their answer.

    if (again == 'Y' || again == 'y')// declarring their answer in either case
                    system("cls");// clearing the screen
                else if (again == 'N' || again == 'n')// declarring their answer in either case
                {
                    system("cls");//  clearing the screen
                    play = false;// ending the loop
                    cout << "Thank you for using my fourtune teller I know it gave you the right answer.!\n";
                    
                }
}
if (choice == 2)
{
    cout<<"you chose stock advice" << endl;
    cout<<"enter in how many stocks you own" << endl;
    cin>> useless2;// storing their data but not using it.
    cout<< "enter in the estimated total value of you stocks" << endl;
    cin>> useless3;// storing their data but not using it.
srand((unsigned)time(NULL));
     int theNumber;
      
      
     theNumber = 1 + rand() % 3;
    switch (theNumber)  
{          
case 1:                        
     cout<<"You should definitely buy!"<<endl;                    
     break;        
case 2:
     cout<<"Sell! Sell! Sell!"<<endl;
     break;
case 3:
     cout<<"Heck if I know.  Go ask your stock broker."<<endl;
     break;




default:
     break;

    }
cout << "Do you wish to start again? (Y/N)\n";//asking them if they want to start again.
                cin >> again;//storing their answer.
    if (again == 'Y' || again == 'y')// declarring their answer in either case
                    system("cls");// clearing the screen
                else if (again == 'N' || again == 'n')// declarring their answer in either case
                {
                    system("cls");//  clearing the screen
                    play = false;// ending the loop
                    cout << "Thank you for using my stock advice i hope it serves you well!\n";// thanking them for playing
                    
                }
}



}

}

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Not Taking User Input
11 Apr, 2007 - 08:39 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Try the following:
CODE

//These two libraries must be used when you want random numbers.  
#include<iostream>
#include <stdlib.h>  
#include <time.h>
#include <string>
#include <limits>
using namespace std;
bool play = true;
int choice;
char useless[200];
int useless2;
int useless3;
char again;
int main()
{    
    while (play == true)
    {    
        cout<< "welcome to my Project you will either get your fortune or you stock advice" << endl;
        cout<< "chooose one"  << endl;
        cout<< "1. fourtune teller" << endl;
        cout<< "2. stock advice" << endl;
        cin>> choice;
        if (choice == 1)
        {
            cout << "hi and welcome to my fortune teller" << endl;// describing how its going to work
            cout << "it works like a magic eight ball program" << endl;
            cout << "please enter in a question and it will tell you the answer.";
            
            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            
            cin.getline(useless, 200);
            cout << flush;
             srand((unsigned)time(NULL));
             int theNumber;
          
    
          
             theNumber = 1 + rand() % 4;
            if (theNumber == 1)
            {
             cout<<"No way."<<endl;                    
            }      
            if (theNumber == 2)
            {
                 cout<<"Most likely, yes."<<endl;
            }  
            if (theNumber == 3)
            {
                 cout<<"Try again tomorrow."<<endl;
            }  
            if (theNumber == 4)
            {
            cout<<"Yes, I believe so!"<<endl;
            }
            cout << "Do you wish to start again? (Y/N)\n";//asking them if they want to start again.
            cin >> again;//storing their answer.

            if (again == 'Y' || again == 'y')// declarring their answer in either case
            {
                system("cls");// clearing the screen
            } else if (again == 'N' || again == 'n')// declarring their answer in either case
            {
                system("cls");//  clearing the screen
                play = false;// ending the loop
                cout << "Thank you for using my fourtune teller I know it gave you the right answer.!\n";
        
            }
        }
        if (choice == 2)
        {
            cout<<"you chose stock advice" << endl;
            cout<<"enter in how many stocks you own" << endl;
            cin>> useless2;// storing their data but not using it.
            
            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

            cout<< "enter in the estimated total value of you stocks" << endl;
            cin>> useless3;// storing their data but not using it.

            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

            srand((unsigned)time(NULL));
            int theNumber;
      
      
            theNumber = 1 + rand() % 3;
            switch (theNumber)  
            {          
                case 1:                        
                     cout<<"You should definitely buy!"<<endl;                    
                     break;        
                case 2:
                     cout<<"Sell! Sell! Sell!"<<endl;
                     break;
                case 3:
                     cout<<"Heck if I know.  Go ask your stock broker."<<endl;
                     break;
                default:
                     break;

            }
            cout << "Do you wish to start again? (Y/N)\n";//asking them if they want to start again.
            cin >> again;//storing their answer.
            if (again == 'Y' || again == 'y')// declarring their answer in either case
            {
                system("cls");// clearing the screen
            }else if (again == 'N' || again == 'n')// declarring their answer in either case
            {
                system("cls");//  clearing the screen
                play = false;// ending the loop
                cout << "Thank you for using my stock advice i hope it serves you well!\n";// thanking them for playing
    
            }
        }

    }
    return 0;
}


The problem with mixing inputs of numbers and strings is that when you cin << number the stream leaves the CR in the buffer, so that when you input a string it stops at the CR and gives you an zero length string with no wait. I am sure that if I were not so tired I would know WHY it does things this way, but at the moment it just seems like a bug. But that is the way it works.

To solve this use two lines:

CODE

cin.clear() //clears the input buffer..
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Will ignore all CR's until EOF (well EO-Stream at least).



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:27PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month