Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   C++ question (http://www.programmingforums.org/showthread.php?t=11666)

physicist Oct 22nd, 2006 7:17 PM

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?

Prm753 Oct 22nd, 2006 7:36 PM

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.

physicist Oct 22nd, 2006 8:05 PM

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.....

physicist Oct 22nd, 2006 9:03 PM

i tried combining the strings prm753 but it still wont compile or link

Sane Oct 22nd, 2006 9:16 PM

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.

Dietrich Oct 22nd, 2006 9:27 PM

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!

physicist Oct 22nd, 2006 10:14 PM

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....

The Dark Oct 22nd, 2006 10:25 PM

Quote:

Originally Posted by physicist (Post 117272)
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.

Seif Oct 23rd, 2006 7:39 AM

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());


physicist Oct 23rd, 2006 7:49 PM

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;
}



All times are GMT -5. The time now is 1:09 AM.

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