Click here to Skip to main content
15,903,362 members
Home / Discussions / Algorithms
   

Algorithms

 
AnswerRe: HI ALL Pin
73Zeppelin29-Apr-08 20:21
73Zeppelin29-Apr-08 20:21 
GeneralRe: HI ALL Pin
johnzfrebbies@30-Apr-08 2:06
johnzfrebbies@30-Apr-08 2:06 
GeneralRe: HI ALL Pin
David Crow30-Apr-08 3:08
David Crow30-Apr-08 3:08 
AnswerRe: HI ALL Pin
73Zeppelin30-Apr-08 3:19
73Zeppelin30-Apr-08 3:19 
AnswerRe: HI ALL Pin
johnzfrebbies@29-Apr-08 16:43
johnzfrebbies@29-Apr-08 16:43 
GeneralRe: HI ALL [modified] Pin
73Zeppelin29-Apr-08 20:02
73Zeppelin29-Apr-08 20:02 
GeneralRe: HI ALL Pin
johnzfrebbies@30-Apr-08 11:28
johnzfrebbies@30-Apr-08 11:28 
AnswerRe: HI ALL Pin
73Zeppelin1-May-08 0:11
73Zeppelin1-May-08 0:11 
johnzfrebbies@ wrote:
I still have not got the program to display the answers whether correct or incorrect.


I'm not sure why - the code I posted worked perfectly.


johnzfrebbies@ wrote:
I'm also trying to get user input by typing file name&num so I can read in that file and compare it to the answers. also I will finish it off with a MAXSIZE array in case the exam consists of more than 20 questions. what I need the most is some help with the syntax for the input from user that consists of NameNum.dat


If I understand you correctly, you want the user to input the name of the file to be read. We'll be consistent and use ifstream although there are other file functions one could use. We can proceed as follows.

We need to pass a FileName to ifstream. The FileName is, of course the name of the file we want to open for reading.

First we need an array to hold the file name the user inputs. We'll be consistent with the rest of your program and use an array of size 20:
char FileName[20];


At this point you need to use cout and cin to get the filename from the user. I think you can write the code for that. Finally, we'll use your existing Answers ifstream for reading:
ifstream Answers;
Answers.open(FileName);


It's a good idea to check that the file name we entered corresponds to an existing file. We can do that by checking to see if Answers is NULL:

if (!Answers) // error: unable to open file for input
{
	cout << "Cannot open file." << endl;
	system("PAUSE");
	exit(1);
}


Putting it all together:
char FileName[20];
cout << "Enter a filename: ";
cin >> FileName;
ifstream Answers;
Answers.open(FileName);
if (!Answers) // error: unable to open file for input
{
	cout << "Cannot open file." << endl;
	system("PAUSE");
	exit(1);     // exit the program
}


Of course you can modify this to suit your specific needs, but that's just a basic way to read in a file and get input from it. You could do something different than exit the program if the input file name isn't valid. For example, you could prompt the user to enter another name.

Hope that helps!
GeneralRe: HI ALL Pin
johnzfrebbies@1-May-08 8:32
johnzfrebbies@1-May-08 8:32 
GeneralRe: HI ALL Pin
73Zeppelin1-May-08 9:08
73Zeppelin1-May-08 9:08 
GeneralRe: HI ALL Pin
johnzfrebbies@1-May-08 18:49
johnzfrebbies@1-May-08 18:49 
GeneralRe: HI ALL Pin
73Zeppelin2-May-08 22:06
73Zeppelin2-May-08 22:06 
QuestionCan someone who is good at math please check my working!? - Differential equation [modified] Pin
MarkB77722-Apr-08 3:58
MarkB77722-Apr-08 3:58 
AnswerRe: Can someone who is good at math please check my working!? - Differential equation Pin
73Zeppelin22-Apr-08 4:09
73Zeppelin22-Apr-08 4:09 
GeneralRe: Can someone who is good at math please check my working!? - Differential equation Pin
MarkB77722-Apr-08 4:18
MarkB77722-Apr-08 4:18 
AnswerRe: Can someone who is good at math please check my working!? - Differential equation Pin
73Zeppelin22-Apr-08 4:15
73Zeppelin22-Apr-08 4:15 
GeneralRe: Can someone who is good at math please check my working!? - Differential equation Pin
MarkB77722-Apr-08 4:22
MarkB77722-Apr-08 4:22 
AnswerRe: Can someone who is good at math please check my working!? - Differential equation Pin
CPallini22-Apr-08 4:20
mveCPallini22-Apr-08 4:20 
GeneralRe: Can someone who is good at math please check my working!? - Differential equation Pin
MarkB77722-Apr-08 4:23
MarkB77722-Apr-08 4:23 
GeneralRe: Can someone who is good at math please check my working!? - Differential equation Pin
CPallini22-Apr-08 4:26
mveCPallini22-Apr-08 4:26 
AnswerRe: Can someone who is good at math please check my working!? - Differential equation Pin
CPallini22-Apr-08 4:55
mveCPallini22-Apr-08 4:55 
GeneralRe: Can someone who is good at math please check my working!? - Differential equation Pin
73Zeppelin22-Apr-08 4:57
73Zeppelin22-Apr-08 4:57 
GeneralRe: Can someone who is good at math please check my working!? - Differential equation Pin
CPallini22-Apr-08 5:09
mveCPallini22-Apr-08 5:09 
GeneralRe: Can someone who is good at math please check my working!? - Differential equation Pin
MarkB77722-Apr-08 12:54
MarkB77722-Apr-08 12:54 
GeneralRe: Can someone who is good at math please check my working!? - Differential equation Pin
CPallini22-Apr-08 22:54
mveCPallini22-Apr-08 22:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.