Kind of new to programming, and I'm wondering if I can open a file under the name inputed by the user? I was thinking that maybe I should use pointers, but I'm not really sure.My other issue is that I'm trying to open a file, right now called Registration.txt, and under a series of switch cases have the user input data, but ever time i run it, the file just remains blank. Any way I can keep on adding in information every time I run the switch case again without deleting anthing?
This is the area of code that I'm talking about:
void freshmenchoice()
{
ofstream myfile("Registration.txt");
int class1;
cout<<"Which class would you like to add to your schedual?";
cout<<"\n\n1.EML 1001/nIntro to Engineering\n3 credits\nMonday Wednesday Friday\n10:00AM to 10:50 AM.";
cout<<"\n\n2.EML 1234\nBeginning Principles of Engineering \n3 credits\nMonday Wednesday Friday\n12:00PM to 12:50 PM";
cout<<"\n\n3.EML 1333\nIntro to Programming\n3 credits\nTuesday Thursday\n9:00AM to 10:15AM\n";
cout<<"\n\n4.EML 1525\nStructural Design for Engineers\n3 credits\nMonday Wednesday Friday\n2:00PM to 2:50PM \n";
cout<<"\n\n5.EML 1303\nEngineering Theory\n3 credits\nTuesday Thursday \n12:00PM to 1:15PM"<<endl;
cin>>class1;
switch (class1)
{
case 1: {//ofstream myfile;
myfile.open("Registration.txt");
myfile <<"EML 1234\nBeginning Principles of Engineering \n3 credits\nMonday Wednesday Friday\n12:00PM to 12:50 PM";
myfile.close();}
break;
case 2: {//ofstream myfile;
myfile.open("Registration.txt");
myfile <<"EML 1234\nBeginning Principles of Engineering \n3 credits\nMonday Wednesday Friday\n12:00PM to 12:50 PM";
myfile.close();}
break;
case 3:{//ofstream myfile;
myfile.open("Registration.txt");
myfile <<"EML 1333\nIntro to Programming\n3 credits\nTuesday Thursday\n9:00AM to 10:15AM\n";
myfile.close();}
break;
case 4: {//ofstream myfile;
myfile.open("Registration.txt");
myfile <<"EML 1525\nStructural Design for Engineers\n3 credits\nMonday Wednesday Friday\n2:00PM to 2:50PM \n";
myfile.close();}
break;
case 5: {//ofstream myfile;
myfile.open("Registration.txt");
myfile <<"EML 1303\nEngineering Theory\n3 credits\nTuesday Thursday \n12:00PM to 1:15PM";
myfile.close();}
break;
}
int a;
cout<<"Would yo;u like to register for another class?\a1.Yes\n2.No";
cin>>a;
switch (a)
{
case 1: freshmenchoice();
break;
case 2:
break;
}
}
Thank you!
And can I globably define and open the file before comin to this function with the string name, and make it work like that? I'm just wondering how far i can push it.
as sname is my string that takes user inputs from their First and Last name to a complete name, but this is done in the main() function. When trying to call sname in this side function, it gives me an error
error C3867: 'std::basic_string<_Elem,_Traits,_Ax>::c_str': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Ax>::c_str' to create a pointer to member
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
I'm guessing i should create a pointer in this function that calls the value of the actual variable in the main function before using it?
Whenever you see function call missing arugment list that means that you didn't put the parentheses and/or the arguments.
In this case c_str doesn't need arguments but it needs parentheses to call the function.