stuck on input file

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
Do you have a question?
yes how would i make an input file function that pulls variables from the text file?
It works the same way as with reading in from std::cin, there is even a tutorial on this site:
http://www.cplusplus.com/doc/tutorial/files/
i know that part. what im stuck on is after i infile >> score;
my variable comes out to some crazy -9.89034 or something.
Did the file open successfully?
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
kaiken wrote:
I can open successfully
How do you know?
Edit: bad advice deleted, thanks LB
Last edited on
@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
i figured it out. i had a few things wrong in my code.
Topic archived. No new replies allowed.