a string to an array of characters

I need help with some things.

Could someone give me an practical example on how to convert a string to an array of characters in "void listUsers()"?

Also could someone give me an example on how to make my program not remove the first character in void addUser()"?

I have gone through hundreds of forums and internet pages and found alot but nothing that i could grasp and understand. My wish for this program is to make it work first and then go over it and figure out what i have done badly and what needs improving.

I'm very new, couple of weeks into learning and i am just wondering if theres is any major "DONT'S" in my program that i should unlearn at once?

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int reset = 0;

int listView() // Purpose of this part is to find out what the user entered and return the value to int main()
{
int menuVar = reset;
bool loopTrigger = false; // Will stay false until menuVar gets the value 1, 2 or 3

cout << "\n 1: Add user to database\n";
cout << " 2: List users in database\n";
cout << " 3: Exit\n\n";

for(; loopTrigger == false ;) // Loop for finding what value the user entered
{
if(menuVar != 1)
{
if(menuVar != 2)
{
if(menuVar != 3)
{
cout << "Option: ";
cin >> menuVar;
}
else
{
loopTrigger = true;
}
}
else
{
loopTrigger = true;
}
}
else
{
loopTrigger = true;
}
}

return(menuVar);
}

void addUser() // Adds input from the user into userbase.dat so it looks like this in the file userName:userAdress:userTelephone
{
string userName, userAdress, userTelephone;
ofstream userBase("userbase.dat");

/* This part of the program workds great except for one thing which is that after the first value has been entered and one gets
to enter the second value the program erases the first character in the input. Found hundreds of sites and forums explaining
why this happends but none that explained how to fix it in a way i could understand it.*/

cout << "\nPlease enter new users name: ";
cin.ignore();
getline(cin, userName);
cout << "Please enter new users adress: ";
cin.ignore();
getline(cin, userAdress);
cout << "Please enter new users telephone number: ";
cin.ignore();
getline(cin, userTelephone);

userBase.open("userbase.dat");
if(userBase.is_open())
{
userBase << userName << ":" << userAdress << ":" << userTelephone;
userBase.close();
}
else
{
cout << "\nError opening userbase.dat\n";
}
}

void listUsers() // Is suppose to read a line from the userbase.dat file so that we can output it to the user
{
ifstream userBase("userbase.dat");
string tempLine;

userBase.open("userbase.dat");
if(userBase.is_open())
{
while (! userBase.eof() )
{
getline(userBase, tempLine);
}
userBase.close();
}
else
{
cout << "\nError opening userbase.dat\n";
}


/* Converting string input from file to char array

Having major problems understanding how charanters in a string is moved over to char variable.
Are they just referenced in the char variable char *cp or ...
Need someone to give me a practical implementations into my program and not someone elses.. */


string lineEdit = tempLine;
char ch[] = "test1 test2 test3";
char *cp = ch;
cp = lineEdit.c_str();

}

int main()
{
int menuChosen = reset;

cout << "\n\n****************************************************\n";
cout << "*** Program for testing out new learned things ***";
cout << "\n****************************************************\n\n\n\n";

cout << "Simple user database";

for(; menuChosen != 3 ;)
{
menuChosen = listView();
if(menuChosen == 1)
{
addUser();
}
else if(menuChosen == 2)
{
listUsers();
}
}

return(0);

}

All help and sugestions are appreciated.
For starters, the global variable reset isn't needed.
Use .c_str().
Topic archived. No new replies allowed.