trying to get user input into an array

well i hope someone can help, i have written a program and i had to pass an array into a function and now i have a variable for the subscript of the array and i was the user to input data, specificlly a string, more specifically first and last name without having to create two arrays, i have to do this with other things in the program as well so if i can get help that would be awsome, here is part of the program:

int addFunc(const int totNum, string city[],string state[],string street[],string name[],
int addNum[],int zip[],int telNum[],double bal[],int dateLp[], int addCount, int accNum, int usNum)//function will add a new account
{
int countAf = 1;
if (usNum < 20)
{
while (addCount == 1) //ensures to loop if user wants to input another new account
{/*account account(usNum + 1);
account(usNum).*/
cout << "Add an account: " << endl<<"current account number is #"<<accNum<<"(save this number for reference)"<<endl;
cout << "1.Name: ";
cin>>name[usNum]; //this is the part im trying to do

it should be able to take firstname space lastname
operator >> reads space delimeted values.
To read until the end of the line you need to use std::getline(std::cin, /*...*/)
http://www.cplusplus.com/reference/string/string/getline/

I suggest you to use an array (or better yet a vector) of structs to avoid bunch of arrays and variables: your function can be rewritten as:

int addFunc(std::vector<record>& values)
thank you, i need to read on this some more, to expand on my issue, i am in a class and we are only part way through the book so i am still learning, i have actually spent many hours writing the who program and would like to try to finish it without re writing the whole program, it is overdue so this is not something that is possible for me anyways. How would i use that to read a subscript value? could you show exactly please and thank you very much, this part has been very tricky for me.
additionally i will have to do this with multiple arrays, to expand on the project, i need to write a program that will create users accounts with information, give an option to view the information for any user and edit it as well
Hey i actually see what your talking about now with a structure that uses arrays to store multiple instances of an account, based on the different customers, what would a basic set up look like if you dont mind showing me? thank you
closed account (j3Rz8vqX)
Example of getline:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int addFunc(const int totNum, string city[],string state[],string street[],string name[], 
            int addNum[],int zip[],int telNum[],double bal[],int dateLp[], int addCount, 
            int accNum, int usNum)//function will add a new account
{
    int countAf = 1;
    if (usNum < 20)
    {
        while (addCount == 1) //ensures to loop if user wants to input another new account
        {/*account account(usNum + 1);
            account(usNum).*/
            cout << "Add an account: " << endl<<
                    "current account number is #"<<
                    accNum<<"(save this number for reference)"<<
                    endl;
                    
            cout << "1.Name: ";
            getline(cin,name[usNum]); //Assuming <string> included 

Or
1
2
3
4
5
            string temp;
            cout << "1.Name: ";
            cin>>name[usNum];
            cin>>temp;
            name[usNum]=name[usNum]+temp;


http://www.cplusplus.com/reference/string/string/operator+/

The prior is prefer though...
Last edited on
Topic archived. No new replies allowed.