![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 146
Rep Power: 2
![]() |
C++ question
i got v0.1a working theres a link in my other thread here.
workin on v0.1b and this line doesnt work ofstream myfile2("//"d"//answers.txt");d is a string which was input by user (their name) this is the error 23 C:\Dev-Cpp\TestApp0.1b\test3-1.cpp expected `)' before "d" |
|
|
|
|
|
#2 |
|
Professional Programmer
|
It looks like you are trying to create a folder with a username with the answers inside it. Make your string combined with "answers.txt" before you create the file. // is a comment, \\ is for file paths.
__________________
The world's first athletic computer geek! The home of PrProgramsStudios How not to post a question: <-- Please don't reply |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 146
Rep Power: 2
![]() |
exactly! thanks
now, it still doesnt work when i change // to \\ and if the string combines then it is not in a new folder just a file with a diff. name AND i get an error if i try to specify the path as just a string... so how can i make it so that when the user enters their name a subdir named as the string they just entered in with a .txt is made..... Last edited by physicist; Oct 22nd, 2006 at 7:16 PM. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 146
Rep Power: 2
![]() |
i tried combining the strings prm753 but it still wont compile or link
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
I'm a little lost with what you're trying to do, but have you tried the following yet?
ofstream myfile2("..\\answers.txt");By the way, your signature is wrong. sqrt(x^2) does not equal x. It equals |x|. Therefore, sqrt((-1)^2) equals |-1|, or 1. |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
sqrt(x^2) does not equal x. It equals |x|.
Therefore, sqrt((-1)^2) equals |-1|, or 1. I would say Sane saved the world from being overrun by thousands of mad math teachers!
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 146
Rep Power: 2
![]() |
i know its wrong...its a joke (sorry if it was too simple)
anyway sane, thats not what im tryin to do. i am trying to specify the file path using a string of what the user entered as his name plus answer.txt. and it doesnt work. evidently i cannot specify a file name in the fstream parameter as a variable representing a string.... Last edited by physicist; Oct 22nd, 2006 at 9:26 PM. |
|
|
|
|
|
#8 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
Quote:
Also try to make your topic titles a bit more informative, we can guess that it is a "C++ question" as it is in the C++ forum. Maybe something like "how to add strings together" or "how do I get the user to input a directory". Similarly, your other topic "new question now" is just as uninformative. |
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
Another problem is that you are trying to pass the open() function a string, when in actual fact it takes a character array const char *. So either use a character array to store your file path, or use the c_str() member function of your current string to convert it to a char array.
for example: string filepath = "C:\\"; string filename = "answers.txt"; string file = filepath + filename; ofstream myfile2(file.c_str()); |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 146
Rep Power: 2
![]() |
ok this compiles fine but when i enter the name it jsut freezes...
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int s;
string a,b,c,d,e,f;
f="answers.txt";
cout <<"Enter name";
getline (cin,d);
ifstream myfile("example.txt");
d+f=e;
ofstream myfile2(e.c_str());
ofstream myfile3("namelist.txt");
myfile3 <<d;
while (! myfile.eof() )
{
getline (myfile,a);
cout << a << endl;
}
cout <<"Answer ALL Questions. Follow Directions and mark each answer \nwith the question number in front of it:\n";
cout <<"Example Answers: 1)50mA 2)Dublin 3)iostream.h\n";
cout <<"NOTE: DO NOT PRESS ENTER UNTIL YOU HAVE ANSWERED ALL QUESTIONS. \nAnswer just like the example above.\n";
getline (cin,b);
myfile2 <<b;
cout <<"All Answers to all questions submitted. \nDONT WORRY IF YOU MADE A MISTAKE, SIMPLY RESTART THE PROGRAM \nAND YOUR ANSWERS WILL BE MODIFIED!\n";
myfile2.close();
myfile.close();
system("PAUSE");
return 0;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Attitudes | Oddball | Coder's Corner Lounge | 29 | Mar 18th, 2006 9:34 PM |
| Question about passing objects into member functions | aznluvsmc | C++ | 7 | Sep 27th, 2005 11:41 PM |
| How to post a question | nnxion | C++ | 10 | Jun 3rd, 2005 11:53 AM |
| How to post a question | nnxion | C++ | 0 | Jun 3rd, 2005 8:55 AM |
| How to post a question | nnxion | C | 0 | Jun 3rd, 2005 8:55 AM |