Oct 21, 2013 at 2:34am UTC
okay im making a math game program that simple and need to have a user input a name and then that name.txt is brought up and use variables from the text to add to their score. heres my code kinda long.
#include <iostream>
#include <cmath>
#include <cctype>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdio.h>
#include <time.h>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
string FirstName;
srand ((unsigned)time(0));
char continu = ' ';
int numb;
int sum;
int ssum;
int mult;
int quo;
double score=0;
double correct=0;
double incorrect=0;
ifstream inFile;
ofstream outFile;
cout << "**********************" << endl;
cout << "*** THE MATH GAME ****"<< endl;
cout << "*** Welcome ****"<<endl;
cout << "*** created By ****"<< endl;
cout << "***Nickolas ALeman****" << endl;
cout << "**********************" << endl;
cout << "**********************" << endl;
cout << "y/Y to continue, any other char to exit" << endl;
cin >> continu;
cout << "Please enter your name" << endl;
cin >> FirstName;
//outFile.open(FirstName+".txt",ios::app); this is my attempt not workin
//inFile.open(FirstName+".txt");
//if (inFile == false)
//{
// score = 0;
// incorrect = 0 ;
// correct =0;
//}
//else
//{
// inFile.getline( score );
// inFile.getline( correct );
// inFile.getline( incorrect );
//}
while (continu == 'y' || continu =='Y' )
{
unsigned a =rand() % 10 +1;
unsigned b =rand() % 10 +1;
sum = a+b;
ssum = (a)-(b);
mult = a*b;
quo = (a*b)/b;
cout << setprecision(2);
cout << endl;
cout << "Welcome " << FirstName << endl;
cout << "********CHOOSE A PROBLEM*********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cout << "******** *********" << endl;
cout << "******** 1. ADD *********" << endl;
cout << "******** 2. Subtract *********" << endl;
cout << "******** 3. Multiply *********" << endl;
cout << "******** 4. Divide *********" << endl;
cout << "******** 5. Stats *********" << endl;
cout << "******** 6. QUIT *********" << endl;
cout << "******** *********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cin >> numb;
switch (numb)
{
case 1:
cout << a << "+" << b << "=?" << endl;
cin >> numb;
if (numb ==sum)
{
cout << "Correct!!!" << endl;
cout << fixed << showpoint;
score = score + .05;
correct = correct +1;
break;
}
else
{
cout << "Wrong." << endl;
cout << fixed << showpoint;
score = score - .03;
incorrect = incorrect +1;
break;
}
case 2:
cout << a << "-" << b << " =?" << endl;
cin >> numb;
if (numb ==ssum)
{
cout << "Correct!!!" << endl;
cout << fixed << showpoint;
score = score +.05;
correct = correct +1;
break;
}
else
{
cout << "Wrong" << endl;
cout << fixed << showpoint;
score = score -.03;
correct = correct +1;
break;
}
case 3:
cout << a << "*" << b << "=?" << endl;
cin >> numb;
if (numb == mult)
{
cout << "Correct!!" << endl;
cout << fixed << showpoint;
score = score + .05;
correct = correct +1;
break;
}
else
{
cout << "wrong" << endl;
cout << fixed << showpoint;
score = score - .03;
correct = correct +1;
break;
}
case 4:
cout << a*b << "/" << b << "=?" << endl;
cin >> numb;
if ( numb == quo)
{
cout << "Correct!!" << endl;
cout << fixed << showpoint;
score = score + .05;
correct = correct +1;
break;
}
else
{
cout << "wrong" << endl;
cout << fixed << showpoint;
score = score - .03;
correct = correct +1;
break;
}
case 5:
cout << fixed << showpoint;
cout << "**************************************" << endl;
cout << "**************************************" << endl;
cout << "******" << FirstName; cout << setw(28); cout << "*********" << endl;
cout << "******Total earnings ="<< score; cout <<setw(12); cout << "*********" << endl;
cout << "******Total correct = " << correct; cout <<setw(12); cout << "*********" << endl;
cout << "******Total Incorrect =" << incorrect; cout << setw(11); cout << "*********" << endl;
cout << "**************************************" << endl;
break;
case 6:
//outFile << score << endl;
//outFile << correct << endl;
//outFile << incorrect << endl;
//inFile. close();
//outFile.close();
return 0;
cout << "Welcome " << FirstName << endl;
cout << "********CHOOSE A PROBLEM*********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cout << "******** *********" << endl;
cout << "******** 1. ADD *********" << endl;
cout << "******** 2. Subtract *********" << endl;
cout << "******** 3. Multiply *********" << endl;
cout << "******** 4. Divide *********" << endl;
cout << "******** 5. Stats *********" << endl;
cout << "******** 6. QUIT *********" << endl;
cout << "******** *********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cin >>numb;
}
}
//outFile << score << endl;
//outFile << correct << endl;
//outFile << incorrect << endl;
//inFile. close();
//outFile.close();
return 0;
}
Last edited on Oct 21, 2013 at 2:34am UTC
Oct 21, 2013 at 2:40am UTC
yes how would i make an input file function that pulls variables from the text file?
Oct 21, 2013 at 2:43am UTC
i know that part. what im stuck on is after i infile >> score;
my variable comes out to some crazy -9.89034 or something.
Oct 21, 2013 at 2:57am UTC
Did the file open successfully?
Oct 21, 2013 at 5:38am UTC
I can open successfully but when I put into a double its comes out with a huge number luke -9.258037 •e27 or something like that. For example when I open the txt file with the numbers just being .05. I open it I try to declare what came from the input as double and use it regularly in my code but the number are crazy and don't make sense
Last edited on Oct 21, 2013 at 5:55am UTC
Oct 21, 2013 at 9:34pm UTC
Edit: bad advice deleted, thanks LB
Last edited on Oct 21, 2013 at 9:59pm UTC
Oct 21, 2013 at 9:51pm UTC
@newbieg: on most operating systems, such as Windows, a file can be opened in read-only mode multiple times without issue. I don't know what you were thinking with your notepad example ;p
Oct 22, 2013 at 10:12am UTC
i figured it out. i had a few things wrong in my code.