-
Problem with for loop
Hello. I am having a problem with a program I am making. It won't compile. Can somebody look at it and see what I am doing wrong? (it says that there is a problem with the for loop)
Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <windows.h>
#include <fstream>
using namespace std;
int mainmenu () {
cout<< "Welcome to the NaNoWriMo Notepad!\nHave fun!\n";
cout<< "\nMAIN MENU\n 1 Create a New Project\n 2 Open a Project\n 3 quit\n\n";
int mainmenu;
cin>> mainmenu;
return(mainmenu);
}
void create () {
cout<< "What is your project's name (you can't change this later) (hint: you need to rememer this, so call it the year): ";
char filename[20];
cin>> filename;
cout<< "\nYour file is called " << filename << "\n";
cout<< "Your file is now being created.\n";
ofstream Project(filename, ios::out);
}
int main(int argc, char *argv[])
{
ofstream fout;
system("color 02");
SetConsoleTitle("NaNoWriMo Notepad");
for(,,) {
int menu;
menu = mainmenu();
if(menu == 1) {
create ();
}
else if(menu == 2) {
char myfile[20];
cout<< "Enter the name of your file (the year): ";
cin>> myfile;
}
else if(menu == 3) {
system("exit");
}
}
cout<<"Hello World!\n";
string test;
getline( cin, test );
cout<< test;
system("PAUSE");
return EXIT_SUCCESS;
}
Thanks
-
should be
although it should really be a while loop.
-
Well first lets look at this code and what can be gotten rid of... such as the whole cout<<"Hello World"
You are trying to make an infinate loop with for(,,) but you got it wrong, you want for(;;) but generally if you need to do that in small basic programs you are doing something wrong design wise.
Perhaps you can make a variable (bool run = true) then have a while(run) loop instead of the for loop. That way you don't have to make a call to system("exit") which is another frowned on programming practice.
[edit] 99% of times system() calls can be completely avoided, don't use them unless you have a REALLY good reason to. You could just replace system("exit"); with return 0; and get the same results. Only the return 0; Allows the program to exit naturally.
-
Oh, thanks! I knew it was something simple... :)