Stream / Open File Problems

I am in a C++ class and having problems with the following problem:

Given the availability of a file named numbers write the statements necessary to read an integer from standard input and then read in that many values from numbers and display their total.

The best that I can think of is:

int num;ifstream outFile;outFile.open("numbers");cin >> num;total = 0outFile.close();int a, sum=0;for(int i=0; i<num; i++){sum = sum+=a;}cout << sum << endl;


Any help?
Please edit your post to use code tags and one statement per line.
What do you mean?
You mean for all the digital files and? I help you change a bit. Sorry, my English is very poor.
#include <iostream.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int getFileSize(string &sFileName)
{
struct stat buf;
int iRet = stat(sFileName.c_str(), &buf);
if (iRet == -1)
return NULL;
return buf.st_size;
}
int main()
{
int num;
iofstream File;
File.open("numbers");
int a;
for(int i=0;i<getFileSize("numbers");i++)
File>>a,sum+=a;
cout<<sum;
return 0;
}
This code does not compile:
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
// Non standard iostream call
// #include <iostream.h>
#include <iostream>
// Need to include file stream header
#include <fstream>
#include <sys/types.h> 
#include <sys/stat.h> 
#include <unistd.h>
// Including std namespace
using namespace std;
// You specified a string datatype without using the std namespace
// Also can not pass a constant string literal as a reference
// int getFileSize(string &sFileName)
// int getFileSize(string sFileName)
{
struct stat buf;
int iRet = stat(sFileName.c_str(), &buf);
if (iRet == -1)
// NULL is not a valid int value
// return NULL;
return 0;
return buf.st_size;
}
int main()
{
// Should initialize num
// int num;
int num = 0;
// iofstream is not valid
// iofstream File;
// File.open("numbers");
// ifstream for input from file
ifstream File("numbers");
int a;
for(int i=0;i<getFileSize("numbers");i++)
// sum was not defined in this scope
// File>>a,sum+=a;
// cout<<sum;
// Possibly meant
File>>a,num+=a;
cout<<num;
return 0;
}


There are numerous errors in the above code that have been fixed. I didn't have a numbers folder to open and check, but typically we append .txt to our files. Also, I believe a lot of the errors stem from either lack of trying to run it before posting it, or having a very very outdated compiler. I do suggest upgrading immediately.

Edit: This is a long way:
for(int i=0;i<getFileSize("numbers");i++)
To do this:
while (!File.eof())
Last edited on
Here is what I just input and it still won't compile

#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
{
struct stat buf;
int iRet = stat(sFileName.c_str(), &buf);
if (iRet == -1)
return 0;
return buf.st_size;
}
int main()
{
int num = 0;
ifstream File("numbers");
int a;
for(int i=0;i<getFileSize("numbers");i++)
File>>a,num+=a;
cout<<num;
return 0;
}


Here is what the CodeLab says for the compiler error message:

1 #include <iostream>
2 #include <fstream>
3 #include <cstring>
4 #include <cmath>
5
6 #include <iomanip>
7
8 using namespace std;
9
10 int main () {
11

12 #include <iostream>
13 #include <fstream>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 using namespace std;
18 {
19 struct stat buf;
20 int iRet = stat(sFileName.c_str(), &buf);
21 if (iRet == -1)
22 return 0;
23 return buf.st_size;
24 }
25 int main()
26 {
27 int num = 0;
28 ifstream File("numbers");
29 int a;
30 for(int i=0;i<getFileSize("numbers");i++)
31 File>>a,num+=a;
32 cout<<num;
33 return 0;
34 }

35
36
37 }



Any suggestions?

Please edit your post to use code tags and one statement per line.

Code tags are the "<>" formatting button to the right of the text entry area.
Highlight your code, then click the <> button.

Here is what the CodeLab says for the compiler error message:

What error message?

Which is your code, the first or the second?

Assuming the first block of code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <sys/types.h> 
#include <sys/stat.h> 
#include <unistd.h>
using namespace std;
{
struct stat buf;
int iRet = stat(sFileName.c_str(), &buf);
if (iRet == -1)
return 0;
return buf.st_size;
}
int main()
{
int num = 0;
ifstream File("numbers");
int a;
for(int i=0;i<getFileSize("numbers");i++)
File>>a,num+=a;
cout<<num;
return 0;
}


At line 7, your function header is missing.
int getFileSize (string sFileName)
Adding in the above line, the code compiles cleanly, although it may not be doing what you want. The problem statement says to read the number of values from standard input (cin), that is not the same as obtaining the eof of the file via the stat call.


I have no clue what you're trying to show with the second block of code.

I have no clue what you're trying to show with the second block of code.

That is his real code. There are a lot of errors because he read my post and removed all of the comments, but when he pasted it to his code, he already had some header files in there and the call to main.

@jcoombs
I reposted amchinese's code because I wanted to show how wrong they were. Do not use that code since there are a plethora of issues with it. Even after I corrected all of the compiler errors, there is still a lot of things that you clearly don't understand. We're not here to do your homework for you. We will, however, attempt to aide you with as much help as possible, given that you are making a valiant effort to do this on your own, and given that you're actually reading what we're posting.

With that being said, I know you're new, and I would like to help you so do two things to help me help you.

1) Post your code. You can either copy and paste it here, highlight your code, then press the < > button to the right of the edit box to format your code, or go to pastebin.com or codepad.org and paste your code there. Once you post it online, share the URL to your with use. No formatting is required to post a link here.

2) Explain, in detail, what you want to do, what you have tried to do, and what you're unable to do. Also explain to us anything you're having issues with, and if there is something you don't understand about your code, myself or someone else will gladly look over it, point out key points on it, and attempt to explain it to you as best as possible.

I look forward to hearing back from you so we can get this completed for you and hopefully teach you somethings in the process.
The question for the class is this:
Given the availability of a file named numbers write the statements necessary to read an integer from standard input and then read in that many values from numbers and display their total.

Here is what I started out with on my own:

1
2
3
4
5
6
7
8
9
int num;
ifstream outFile;
outFile.open("numbers");
cin >> num;
total = 0outFile.close();
int a, sum=0;
for(int i=0;
 i<num; i++){sum = sum+=a;}
cout << sum << endl;


And the online hw complier had an error message that said
"CTest.cpp:11:70: error: invalid suffix "outFile.close" on integer constant
CTest.cpp: In function ‘int main()’:
CTest.cpp:11:62: error: ‘total’ was not declared in this scope"


I am having problems understanding what exactly is needed for this problem because this is one of the first times I have worked with C++.

Thanks for your help
You have the general idea.

A few problems though.
1) No main function header.
2) No #include for fstream, or using namespace std.
3) The code you posted does not appear to be what you compiled. i.e. There is no line 11.
4) Line 5 is missing a semicolon after the 0;
5) You don't want to close the file at line 5. You haven't read from it yet.
6) At line 8, you want to read a number from the file (inside the loop).
7) You need a #include for iostream to use cin and cout.
8) At line 5, total is not defined. Did you mean this as a declaration? Not sure what you intended to use total for since it is never referenced.
9) At line 8, the statement doesn't make sense. You want either:
sum = sum + a;
or
sum += a;

BTW, it's a little weird to call you input file outFile, but that won't cause any errors.






Topic archived. No new replies allowed.