This program adds up command line args (all of them ints)

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc,char *argv[]){
fstream a;
for (int q=1; q > argc; q++){
a.open(argv[q]);
}
for (int k=2; k > argc; k++){
}
for (int j=1; j > argc; j++){
a.close(argv[j]);                                 //third and fourth
}
system ("pause");
return 0;
}

1. invalid conversion from char*' to int'
2. invalid conversion from char*' to int'
3. no matching function for call to std::basic_fstream<char, std::char_traits<char> >::close(char*&)'
4. note C:\Dev-Cpp\include\c++\3.4.2\fstream:832 candidates are: void std::basic_fstream<_CharT, _Traits>::close() [with _CharT = char, _Traits = std::char_traits<char>]

the int char seems obvious because they are of different data types, but how else would i be able to get the int outta there?

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Freaky_Chris

argv is an array of strings, not ints. You have to convert strings to ints before adding them answer += atoi(argv[k]); Other ways to accomplish it is to call strtod() or stringstream

You have some very confusing idea's here.

Some of which are potentially dangerous.

for (int q=1; q > argc; q++){
a.open(argv[q]);
}

What do you hope to achieve with this?
Firstly if q ever was greater than argc then you would be stuck in an infinate loop, trying to open a files with a random name, i.e one matching what ever happens to be in some memory address somewhere. Since if argc == 0 that means argv has no data stored within it. So i suggest you scrap that. Also you would only have the final file opened open, just a note.

Secondly

for (int j=1; j > argc; j++){
a.close(argv[j]);                                 //third and fourth
}

You cannot close files like this. a is a file object pointing to a single file. so you close it using a.close(). The method you called doesn't exsist, and once again this is also another case of...if this ever becomes true your in an infinate loop.

I think you should read about file handeling, also i cannot see the point in opening and closing files btw.

Third

for (int k=2; k > argc; k++){
}`