Hi, Can You correct my Looping mistakes ? And my program won't run first part of the coding.
Jul 29, 2015 at 1:03pm UTC
}
Last edited on Aug 6, 2015 at 4:00pm UTC
Jul 29, 2015 at 2:05pm UTC
What do you mean, you can't run it?
Can you please explain what behaviour you're seeing?
Jul 29, 2015 at 2:09pm UTC
Line : 20 (cin>>choice ) is probably the error because its a string. You can't get input of string with cin right?
Jul 29, 2015 at 2:18pm UTC
@ShankRam
You can. It will just crash if the user enters more than one char.
@Fadgo
I can run it without problems.
Jul 31, 2015 at 8:00am UTC
I modified it a bit.
1. I changed the condition for if file is open. To make it readable.
2. move myfile.close() thingy inside the file is open condition.
3. Make a function for clearscreen and pause run to make it easy to modify
4. Make the output fstream / ofstream myfile2. It is used to output and/or append the inputted data of the user.
5. I hope it is useful. Refactor it. Make your condition more precise and short.
Remember:
K-eep
I-t
S-hort and
S-imple
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
void clearScreen(void )
{
system("CLS" );
}
void pauseRun()
{
system("PAUSE" );
}
int main(void )
{
while (1)
{
char choice[2];
cout <<":: Arcade Menu::\n\n"
<<"1. Option 1\n"
<<"2. Option 2\n"
<<"3. Exit Program\n\n"
<<"Choice: " ;
cin >> choice;
if (choice[0] == '1' )
{
clearScreen();
ifstream myfile;
string name;
double salary, mcn ;
cout<<" Option 1\n\n" ;
myfile.open("salaryOutput.txt" , fstream::in);
if (myfile.is_open())
{
while (!myfile.eof())
{
myfile >> name >> mcn;
if (myfile.fail())
break ;
else
cout << name << " " << mcn << endl;
}
myfile.close();
}
else
{
cout<<"Unable to open file!" ;
}
ofstream myfile2;
myfile2.open("salaryOutput.txt" , ofstream::app);
if (myfile2.is_open())
{
int i = 0;
while (i < 5)
{
cout << "Please enter name and member card number: " ;
cin >> name >> mcn;
cout << "adding new record..." << endl;
myfile2 << name << " " << mcn << endl;
i++;
}
cout << "Data Saved" << endl;
myfile2.close();
}
else
{
cout << "Unable to open in append mode!" ;
}
system ("pause" );
clearScreen();
}
else if (choice [0] == '2' )
{
clearScreen();
cout << " Option 2\n\n" ;
pauseRun();
clearScreen();
}
else if (choice [0] == '3' )
{
return 0;
}
else if (choice [0] > '3' || choice[0] <'1' )
{
clearScreen();
cout<<" Invalid Option\n\n" ;
pauseRun();
clearScreen();
}
else
{
clearScreen();
cout<<"Invalid Option\n\n" ;
pauseRun();
clearScreen();
}
}
}
Last edited on Jul 31, 2015 at 8:04am UTC
Topic archived. No new replies allowed.