I made a fortune teller and stock option option program and it works great. The only problem I have is with the getline command. When I enter my question in the fortune teller function it works fine but then I go to the leave function and it displays the answer without any input. I have tryed putting the cin.ignore() in the leave function but it still dosent work.
Thank you for taking you time to read this.
-Nick
Code:
//Workshop 7 Graded Project
//Nick Waylett
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string>
using namespace std;
//prototype
int menu();
int fort();
int stock();
int leave();
//prototype end
////////////////////////////////////////////////////////////////////////////////
int main()//////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
{
//program
menu();
//program end
getch();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
int menu()//Menu function//////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
{
//system
system ("cls");
//system end
//vars
int choice;
//vars end
//program
//Menu
cout<<"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"<<endl;
cout<<"+ Welcome to Nicks Stock Market advice and Fortune Teller Program! +"<<endl;
cout<<"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"<<endl;
cout<<"Select your choice."<<endl;
cout<<"1. Stock Market Advice."<<endl;
cout<<"2. Fortune Teller."<<endl;
cout<<"3.Exit."<<endl;
cout<<"Enter :";
cin>>choice;
//
switch (choice)
{
case 1:
stock();
break;
case 2:
fort();
break;
case 3:
system("cls");
cout<<"Good Bye!"<<endl;
break;
default:
cout<<"Please enter a correct selection."<<endl;
getch();
main();
break;
}
//program end
}
////////////////////////////////////////////////////////////////////////////////
int fort()//fortune teller function/////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
{
//system
system("cls");
//system end
//variables
int number;
string question;
//variables end
srand((unsigned)time(NULL));//random number gen
number=1+rand()%6;//random number gen
cout<<"Question about your future?: ";
getline(cin,question);
cin.ignore();
if(number==1)
{
cout<<"No way dude!"<<endl;
}
else if(number==2)
{
cout<<"Most likely, yes."<<endl;
}
else if(number==3)
{
cout<<"Try again tomorrow."<<endl;
}
else if(number==4)
{
cout<<"Yes, I believe so!"<<endl;
}
else if(number==5)
{
cout<<"I really dont know."<<endl;
}
else if(number==6)
{
cout<<"Never!"<<endl;
}
else
{
cout<<"Error in srand"<<endl;
}
getch();
leave();
//program end
}
////////////////////////////////////////////////////////////////////////////////
int stock()//stocks function.///////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
{
//system
system ("cls");
//system end
//vars
int number;
//vars end
//program
srand((unsigned)time(NULL));//random number
number=1+rand()%3;//random number
switch(number)
{
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:
cout<<"Error in srand"<<endl;
break;
}
getch();
leave();
//program end
}
////////////////////////////////////////////////////////////////////////////////
int leave()//The leave function/////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
{
//colors
system("cls");
//colors end
//variables
char yesorno;
//variables end
//program
cout<<"Would you like to exit? (y or n):";
cin>>yesorno;
switch(yesorno)
{
case 'y':
cout<<"Press any key to exit."<<endl;
system("cls");
cout<<"Good Bye!"<<endl;
break;
case 'n':
cout<<"Press any key."<<endl;
getch();
main();
break;
default:
cout<<"Please enter the correct choice."<<endl;
getch();
system("cls");
leave();
break;
}
//program end
}