Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 22nd, 2006, 6:17 PM   #1
physicist
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 146
Rep Power: 2 physicist is on a distinguished road
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");
only this line; the error has nothing to do with other lines
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"
so basically i cant have a string in my filepath of this newly created text file?
physicist is offline   Reply With Quote
Old Oct 22nd, 2006, 6:36 PM   #2
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
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
Prm753 is offline   Reply With Quote
Old Oct 22nd, 2006, 7:05 PM   #3
physicist
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 146
Rep Power: 2 physicist is on a distinguished road
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.
physicist is offline   Reply With Quote
Old Oct 22nd, 2006, 8:03 PM   #4
physicist
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 146
Rep Power: 2 physicist is on a distinguished road
i tried combining the strings prm753 but it still wont compile or link
physicist is offline   Reply With Quote
Old Oct 22nd, 2006, 8:16 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Oct 22nd, 2006, 8:27 PM   #6
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
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!
Dietrich is offline   Reply With Quote
Old Oct 22nd, 2006, 9:14 PM   #7
physicist
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 146
Rep Power: 2 physicist is on a distinguished road
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.
physicist is offline   Reply With Quote
Old Oct 22nd, 2006, 9:25 PM   #8
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by physicist View Post
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.....
Try using + to add strings together, if d is an std::string object. If it isn't you will have to add the strings together using strcat.

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.
The Dark is offline   Reply With Quote
Old Oct 23rd, 2006, 6:39 AM   #9
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3 Seif is on a distinguished road
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());
Seif is offline   Reply With Quote
Old Oct 23rd, 2006, 6:49 PM   #10
physicist
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 146
Rep Power: 2 physicist is on a distinguished road
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;
}
physicist is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC