program not working please help.

if its not to much to ask ive been learning C, and C++ for a while and im not used to the OOP anyway,

here is the code

// basic file operations
#include <iostream>
#include <fstream>
#include <time.h>
#include <cstdio>
#include <cstdlib>

using namespace std;
string command;
string file;
string username;
////////////////////////////CONSOLE////////////////////////////////////

int save()
{
cout <<"Save game Y/N?\n"<< username <<".CONSOLE: ";
getline (cin, command);
if ( command == "y" )
{
ofstream myfile;
myfile.open (".save");
myfile << "stuff.\n";
myfile.close();
}
if ( command == "n" )
{
//do things
}
return 0;
}

int main ()
{
cout << "\n************************\n* Pseudo3D (c) *\n************************\nUSER.CONSOLE: ";
cout <<"User name?\nUSER.CONSOLE: ";
getline (cin, username);
cout <<"Press enter to begin.\n" << username <<".CONSOLE: ";
fscanf( "%s %s", &command, &file);
if ( command == "save" )
{
cout << save();
}

if ( command == "load" )
{
//load .vtx map "file"
}
return 0;
}

it gives me this error but I dont know why since I used "string" variables not "char*"
"Dell-DV051:~$ g++ -o io console.cpp
console.cpp: In function ‘int main()’:
console.cpp:38:34: error: cannot convert ‘const char*’ to ‘FILE* {aka _IO_FILE*}’ for argument ‘1’ to ‘int fscanf(FILE*, const char*, ...)’
noah@noah-Dell-DV051:~$ g++ -o io console.cpp
console.cpp: In function ‘int main()’:
console.cpp:38:34: error: cannot convert ‘const char*’ to ‘FILE* {aka _IO_FILE*}’ for argument ‘1’ to ‘int fscanf(FILE*, const char*, ...)’"
Last edited on
fscanf is the c way of reading a file. You wanted to use scanf which reads from the keyboard/standard input.

It is also not a wise idea to mix c and c++. Both Languages have file io structures to be used with them. They also have equivalent structures on things to get things done in either text or binary files.

Also learn to use [ code ] and [ /code ] code tags like I have around fscan

Unless you are trying to write out to the file with that line, which would fprintf
Last edited on
thanks I'm new here, yeah first I read a "C for dummies" then "C++ for dummies" guess that gave me bad habits, what would you suggest I use as an alternative to fscan like cin? from what I understood you could not have multiple variables read from it
Last edited on
well all streams in c++ work about the same way, whether it is a a file stream, standard io streams or string streams.

1
2
3
cin/cout are the standard io stream
ifstream/ofstream are file io streams, fstream can be used as well.
stringstream is the string streams.


All of these can be found on the reference page of c++ here:
http://www.cplusplus.com/reference/

All of the above streams are laid out in their order of inheritance:
http://www.cplusplus.com/reference/iostream/
Last edited on
kay thanks I think I got it, i'll figure out here on out (don't you hate those people who want you to do all the work for them :D )
Topic archived. No new replies allowed.