What's the output from this program using fork() and why?

File I/O C++ help plzz?

  • This is just a partial of the code but can anyone help me with CASE 2(calculating the sum)? I keep getting wrong output. Is there something I should add/remove or did I need to do something about the entire code? #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main () { int choice,firstnum; int sum=0; float avg=0; string filename; ifstream inData; ofstream outData; cout<<"Enter the filename: "; cin>>filename; inData.open(filename.c_str()); inData>>firstnum; /*first line of num to indicate the number of numbers to be read*/ char number[firstnum]; inData>>number; do{ cout<<endl; cout<<"Welcome"<<endl; cout<<"1.Print out the list of number from the text file."<<endl; cout<<"2.Compute the sums of the numbers read."<<endl; cout<<"3.Print out the horizontal plot."<<endl; cout<<"4.Arrange the numbers in ascending order."<<endl; cout<<"5.Quit."<<endl; cout<<"Your choice: "; cin>>choice; switch (choice) { case 1: for (int count=0; count<firstnum;count++) { inData>>number[count]; cout<<number[count]<<" "; } break; case 2: for (int count=0;count<firstnum;count++) { inData>>number[count]; sum=sum+(number[count]); } cout <<"The sum is " << sum <<endl; break; } }while (choice!=5); cout<<"Thank you for using this program."<<endl; system ("pause"); return 0; } Example from a text file: 5 //first line indicate the number of numbers to be read 38472 //5 numbers as indicated (sum up these 5 numbers) >Anyone know whats the problem?????

  • Answer:

    Probably you have tried first option 1 and then option 2. If you would have tried option2 first time, you would have got the results. Anyway the basic problem is this. In option 1, you are reading the data from file. So, get pointer in the file would have been moved to eof. Thus, when you have tried option 2 later, you might be getting error. As you have already read data into an array you can change all options such that they use that array. case 2: for (int count=0;count<firstnum;count++) sum=sum+ number[count]; cout <<"The sum is " << sum <<endl; break; If I remember correctly, few days back I have supplied a working program

James Bond at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

char number[firstnum]; //Define an array inData>>number; //Read in ONE! number Print All numbers inData>>number[count]; //You should already have read in all the numbers. cout<<number[count]<<" "; //This should be in a for loop, Sum all numbers inData>>number[count]; //You should already have read in all the numbers. sum=sum+(number[count]); // This should be in a for loop. That will be enough to get you started. Have fun.

AnalProgrammer

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.