pass the array to function!



Hi,

here in this program the void function should attach the numbers that entered by user with (e) and number of index..



void generateNew(string NewID[], int size)
{
for(int x=0;x<size;x++)// for loop to represent the new numbers
{
cout << "The New ID of item " << (x+1) <<
" is e" << x << NewID[x] << endl;
}

}

BUT, the screen display letters instead of the stored numbers !!
how to solve it?
, BTW there is no errors it compiles successfully
Are the numbers stored in NewID? Weird you are storing numbers as strings. How do you receive the numbers from the user?
It looks like you are making your array a string. You should make the array int if you want it to display numbers correctly.

Also, a tip: Make your variable names more meaningful, it helps us understand your process easier.


but in the question they want the array declared as string .!and the user will inter only numbers

the first part of Q is >>Write a C++ program that generates a new user ID from
an array of 7 strings called IDs.

BTW, I've changed it to int and it works .

But, Is it possible to work with string ?
String also can be use to store number.
i been trying to do a program like this, but no problem been found
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main()
{
    string NewID[3];
    int size=3;
    for(int x=0;x<size;x++)
    cin>>NewID[x];
 
    for(int x=0;x<size;x++)// for loop to represent the new numbers
    cout << "The New ID of item " << (x+1) <<" is e" << x << NewID[x] << endl;
    system ("pause");
    return 0;
}

12345
23456
34567
The New ID of item 1 is e012345
The New ID of item 2 is e123456
The New ID of item 3 is e234567
Press any key to continue . . .


If u still having the problem, i guess the problem come from the part where u let the user enter and store the ID... try have a look at there..


yeah , it works with me either because it's all in one function ,

but when the part which attach the number with (e)... in other function it doesn't work with string
dunno why! :/ it works only with int.



void generateNew(string NewID[], int size)
{
for(int x=0;x<size;x++)// for loop to represent the new numbers
{
cout << "The New ID of item " << (x+1) <<
" is e" << x << NewID[x] << endl;
}

}



yeah yeah , the problem was that the input was int and array was string

thats why it gives me letters not numbers ,

I changed it , and it works ,

thanks for all of your kind help :>
Topic archived. No new replies allowed.