Array or Vector Information

Hello,
I am trying to make a small application that will allow me to input a username to populate fields in a file I am creating. The problem that I am having is that the program loops as designed, but does not store more then the last name for the file (in this case it will always be "quit"). I am guessing I need to use an array or vector for the string userName...but am not sure how to go about this. My code is below. Can anyone give me a hint or tip to push me in the right direction? Any help is appreciated, and thank in advance!

Thanks
maximusava

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    system("title CSV CREATOR");
    
    string userName = " ";
    ofstream myfile;


    string csvData (userName);
    {
     do{           
    cout << "Please enter the username: ";
    cin  >> userName;
    
    ofstream myfile;
    myfile.open ("userinfo.csv");
    myfile << "TargetPath,TargetType,Owner,Domain.\n";
    myfile << "\\\\Serverapp16\\g$\\PSTs\\" + userName + ",folder,ourdomain\\" + userName + ",ourdomain.com";
           }while(userName != "quit");
    myfile.close();    
    }  

    system("PAUSE");
    return EXIT_SUCCESS;

}
Just move the myfile.open() outside the do loop.
Thank you jsmith, but that did not seem to solve the problem. When I move the myfile.open() outside the do loop, I ge t the same result. My csv file only has one entry, which looks something like this:

TargetPath TargetType Owner Domain
\\Serverapp16\g$\PSTs\quit folder ourdomain\quit ourdomain.com

Any other thoughts?

Thanks!
Try this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
	system("title CSV CREATOR");

	string csvData;
    string userName;
    ofstream myfile;

	myfile.open ("userinfo.csv");
    {
		do{           
			cout << "Please enter the username: ";
			cin  >> userName;

			if(userName != "quit")
				csvData = csvData + "\\\\Serverapp16\\g$\\PSTs\\" + userName + ",folder,ourdomain\\" + userName + ",ourdomain.com";
		}while(userName != "quit");

		myfile << csvData;

		myfile.close();    
	}  

	system("PAUSE");
	return EXIT_SUCCESS;
}

Thanks jfolk. While that does capture all of the information I am inputting, it does it all on one line. I need to have the information line after line. If I can figure out how to add a carriage return after each item, this may work. Although I have not been able to figure this out yet with an open file? I am still trying and testing...
Change:
1
2
if(userName != "quit")
				csvData = csvData + "\\\\Serverapp16\\g$\\PSTs\\" + userName + ",folder,ourdomain\\" + userName + ",ourdomain.com";

To...
1
2
3
if(userName != "quit")
				csvData = csvData + "\\\\Serverapp16\\g$\\PSTs\\" + userName + ",folder,ourdomain\\" + userName + ",ourdomain.com\r";


I just added: \r to the end of the line. \r is a carriage return.
LOL...how did I miss that! Anyway, that worked perfectly. Thanks so much for all of your help!
I'm confused. When I moved the open outside of the do loop and eliminated the duplicate declaration of myfile,
it worked just fine for me.
Topic archived. No new replies allowed.