Function exit to main

Pages: 12
closed account (o10S216C)
I got a function that enters data to an array. When i call the function i want to store data in element 0 and then exit the function and go back to main. When i call the function for the 2nd time i want it to enter the data in the 1st element and then exit the function and go back to the main. How do i exit the function to go back to main without loosing the spot in the array so when i go back to the function to enter more data in the array i can start off where i left off?
Last edited on
Can you show us the code you have? We'll have a better chance of helping if we can see what you are trying to do.
closed account (o10S216C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    68  void getCustomer(Customer account[], int s)
    69  {
    70     int i = 0;
    71
    72     for(; i < 20; i++)
    73 
    74     {
    75         cout << "nAccount #: " << i  << endl;
    76
    77         cout << "Enter First Name: ";
    78         cin >> account[i].firstName, SIZE;
    79         cout << "Enter Last Name: ";
    80         cin >> account[i].lastName, SIZE;

How do i make it go back to main after line 80 is entered w/out loosing my spot in my array?

Also my array can only store 20 elements or (accounts) in this case.
Last edited on
one way would be a static int.

1
2
static int i=0; instead of int i=0;
i++;

i believe is the correct code however i have recently had trouble using static int's , perhaps if static int i was created outside the function inside main and added to the parameters it could be of some help.

void getCustomer(Customer account[], int s, static int i)

(and dont forget i in the function call as well)


this would keep the spot of the array however the for loop will need a break in it to return to main unless you only had the for loop run once every time the function is called.
Last edited on
closed account (o10S216C)
1
2
3
4
5
6

 error: storage class specifiers invalid in parameter declarations
 error: storage class specified for parameter `i'

those are my errors at my function prototype
 
Last edited on
did you change the function heading along with the prototype and function call? if so how bout a full code post to help me understand the code better
** sometimes i just need to code trial and error style so code will help me see what needs to get kicked around so to speak
Last edited on
closed account (o10S216C)
[code]
I took out other lines so you dont get confused but thats everything that has to do with that function.
Last edited on
one small thing is that in your function call
getCustomer(account,i);
should be
getCustomer(account[],i):

now to have the program run until the array is full (its set to max at 20) try something along these lines

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
33
34
35
36
37
38
39
40
41
42
43
44
19  void getCustomer(Customer account[], int s);

    22
    23  int main()
    24  {
    25     Customer account[20];
    26     int choice;
    27     int i = 1;
    28     int size = 0;
    29     int z=0;
    30     while(z<20)//only runs until 19 but runs 20 times (0-19)
    31     {
    32     cout << " " << endl;
    33     cout << "----------Menu------------" << endl;
    34     cout << "1) Enter Customer Information: " << endl;
    
    41     cin >> choice;

    48
    49     switch(choice)
    50     {
    51     case 1 : getCustomer(account[], i);
    52              break;
             z++;//increments z for while loop
    64  }
    65  return 0;
    66  }
    67  
    68  void getCustomer(Customer account[], int s)
    69  {
    70     int i = 0;
    71  static int g=0//keeps track of array index
    72     for(; i < 1; i++)//loop that runs one time for one "account"
    73  
    74     {
                      
    75         cout << "nAccount #: " << g << endl;
    76  
    77         cout << "Enter First Name: ";
    78         cin >> account[g].firstName, SIZE;
    79         cout << "Enter Last Name: ";
    80         cin >> account[g].lastName, SIZE;
             g++;//increments g to keep track of array index
}



//comments help greatly especially when someone else is trying to debug your code
Last edited on
closed account (o10S216C)
 
75         cout << "nAccount #: " << i  << endl;


so instead of "i" i should change it to "g" to let the user know what account # he is on? correct?
yes the static g will only move up once (with g++;) and keeps its value no matter how many times the function is called ( im sure their are limits ) also you would not necessarily need the while loop inside of the function getCustomer(account[],s) since the code will automatically run once
this would allow you to lose the int i and clean up the code some
Last edited on
closed account (o10S216C)

getCustomer(account,i); i will get an error....
Last edited on
im only saying for the function the below

1
2
3
4
5
6
7
8
9
10
11
      void getCustomer(Customer accoutn[],int s) 
{
             int i = 0;
    72     static int g = 0;
    76         cout << "nAccount #: " << g  << endl;  
    78         cout << "Enter First Name: ";
    79         cin >> account[i].firstName, SIZE;
    80         cout << "Enter Last Name: ";
    81         cin >> account[i].lastName, SIZE;
    94         g++;
}


also something i just ran into would be
1
2
3
4
5
6
7
8
9
10
11
12
40     cout << "Enter 1, 2, 3, 4, 5, or 6: ";
    41     cin >> choice;
    42
    43     while (!cin ||choice < 1 || choice > 6)//!cin allows you to check for char's or string's 
                                                                      //input  into  cin
    44     {
                    cin.clear();
                    cin.ignore(200,'n');
    45        cout << "Invalide Entery, Please Try Again: ";
    46        cin >> choice;
    47     }



and yes the [] are not needed in the function call i misread while looking back on using arrays in parameters


nvm i see the do while loop now didnt notice the brackets and while at bottom of main =x
Last edited on
closed account (o10S216C)
okay ill test that out

but
 
 30     while(z<20)//only runs until 19 but runs 20 times (0-19) 


where would that line go if i wanted to keep my while loop?
closed account (o10S216C)
yes because if the user enters 9 or 10 or -1 or 0 it should repeat the statement

1
2
3
    
47        cout << "Invalide Entery, Please Try Again: ";
48        cin >> choice;


till a number 1-6 is entered
Last edited on
you could go with the !6 || z<19 but it could also be z>18 because the do while loop might run again at 18 depending on where the incrementer is in the loop


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
33
34
35
36
37
38
39
40
41
42
43
44
 23  int main()
    24  {
    25     Customer account[20];
    26     int choice;
    27     int i = 1;
    28     int size = 0;
    29
    30     do
    31     {
    32     cout << " " << endl;
    33     cout << "----------Menu------------" << endl;
    34     cout << "1) Enter Customer Information: " << endl;
    35     cout << "2) Not implemented " << endl;
    36     cout << "3) Not implemented " << endl;
    37     cout << "4) Not implemented " << endl;
    38     cout << "5) Not implemented " << endl;
    39     cout << "6) Exit Program: " << endl;
    40     cout << "Enter 1, 2, 3, 4, 5, or 6: ";
    41     cin >> choice;
    42
    43     while (choice < 1 || choice > 6)
    44     {
    45        cout << "Invalide Entery, Please Try Again: ";
    46        cin >> choice;
    47     }
    48
    49     switch(choice)
    50     {
    51     case 1 : getCustomer(account, i);
    52              break;
    53              z++;
    54     case 2 : //Not implemented
    55              break;
    56     case 3 : //Not implemented
    57              break;
    58     case 4 : //Not implemented
    59              break;
    60     case 5 : //Not implemented
    61              break;
    62     case 6 : cout << "Exiting Program: " << endl;
    63     }
    64  }while(choice != 6 || z<19);
    65  
    66  return 0;


1
2
3
4
5
6
7
8
9
10
11
  cout << "Enter 1, 2, 3, 4, 5, or 6: ";
    41     cin >> choice;
    42
    43     while (!cin ||choice < 1 || choice > 6)//!cin allows you to check for char's or string's 
                                                                      //input  into  cin
    44     {
                    cin.clear();
                    cin.ignore(200,'n');
    45        cout << "Invalide Entery, Please Try Again: ";
    46        cin >> choice;
    47     }


also the code above still catches numbers that are outside of 1-6 but also catches characters entered and throws a flag so the program does not do something crazy. try entering in an "a" with out the code and then try it with the code to seethe effects. never hurts to have extra backup checks even if they never get used
Last edited on
however im not sure that you would need it now that i look at the code more. if you want to run the program until the user says quit (enters 6) then that statement would stay the same, however if you only want the program to run 20 times have that as a limit in your loop hope this helps with that
closed account (o10S216C)
I just want the program at MOST to run 20 times. so the user has 20 elements in a array to fill if they would like too. Would there be an more efficient way of doing so? So every time they get out of a function is brings them back to the "menu"??
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 23  int main()
    24  {
    25     Customer account[20];
    26     int choice;
    27     int i = 1;
    28     int size = 0;
    29     int z =0;
    30     do
    31     {
    32     cout << " " << endl;
    33     cout << "----------Menu------------" << endl;
    34     cout << "1) Enter Customer Information: " << endl;
    35     cout << "2) Not implemented " << endl;
    36     cout << "3) Not implemented " << endl;
    37     cout << "4) Not implemented " << endl;
    38     cout << "5) Not implemented " << endl;
    39     cout << "6) Exit Program: " << endl;
    40     cout << "Enter 1, 2, 3, 4, 5, or 6: ";
    41     cin >> choice;
    42
    43     while (choice < 1 || choice > 6)
    44     {
    45        cout << "Invalide Entery, Please Try Again: ";
    46        cin >> choice;
    47     }
    48
    49     switch(choice)
    50     {
    51     case 1 : getCustomer(account, i);
    52              
    53              z++;//goes above break or it wont work correctly
                      break:
    54     case 2 : //Not implemented
z++;
    55              break;
    56     case 3 : //Not implemented
z++
    57              break;
    58     case 4 : //Not implemented
z++;
    59              break;
    60     case 5 : //Not implemented
z++;
    61              break;
    62     case 6 : cout << "Exiting Program: " << endl;
return 0;
    63     }
    64  }while( z<20);
    65  system("PAUSE");
    66  return 0;



this will run and print the menu 20 times or until the user enters 6

however try taking the menu out of the loop if you do not want it to print everytime

do you have a compiler to try it on if not try dev-c++

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
int choice;
      int i = 1;
      int size = 0;
      int z=0;
 
      do
      {
      cout << " " << endl;
      cout << "----------Menu------------" << endl;
      cout << "1) Enter Customer Information: " << endl;
      cout << "2) Not implemented " << endl;
      cout << "3) Not implemented " << endl;
      cout << "4) Not implemented " << endl;
      cout << "5) Not implemented " << endl;
      cout << "6) Exit Program: " << endl;
      cout << "Enter 1, 2, 3, 4, 5, or 6: ";
      cin >> choice;
 
      while (!cin||choice < 1 || choice > 6)
      {
            cin.clear();
            cin.ignore(200,'n');
         cout << "Invalide Entery, Please Try Again: ";
         cin >> choice;
      }
 
      switch(choice)
      {
      case 1 : getCustomer(account, i);
               break;
               z++;
      case 2 : //Not implemented
              break;
      case 3 : //Not implemented
               break;
      case 4 : //Not implemented
               break;
      case 5 : //Not implemented
               break;
      case 6 : cout << "Exiting Program: " << endl;
      return 0;
      }
   }while(choice != 6 || z<19);
   
   return 0;

}
void getCustomer(Customer accoutn[],int s) 
{
             int i = 0;
      static int g = 0;
          cout << "nAccount #: " << g+1  << endl;  // g+1 so g starts at 1 array starts at 0 
          cout << "Enter First Name: ";
          cin >> account[g].firstName, SIZE;
          cout << "Enter Last Name: ";
          cin >> account[g].lastName, SIZE;
          g++;
}

this is what i got to run
Last edited on
closed account (o10S216C)
can you put it in code please. easier to understand whats going on

do you have "g + 1" just so it can start at element 1 and end up at 20 if needed to?
Last edited on
Pages: 12