for loop and using char as a variable

// help
cout << "enter numbers of names: " << endl;
cin >> iNumberInputs;
for (int x=0; x<iNumberInput; x++){
cout << "name: " << endl;
gets(name[x]);
}
for(int =0; x<iNumberInputs; x++) {
cout <<"the name you just enter is "<< name[x] << endl;
}

// i need a program that would input numbers of name
// but when i try this, it would just print out:

enter number of names: 3
name: ryu
name: ken
name: knight
the name you just enter is k
the name you just enter is n
the name you just enter is i // <= help!

// how can i program it to out put this
the name you just enter is ryu
the name you just enter is ken
the name you just enter is knight

// im using tc++



int number;
cout<<"Enter number of names: ";
cin>>number;

string names[number];

for(int i=0 ; i<number ; i++){
cout<<"\n Enter "<<i+1<<". name: ";
cin>>names[i];
}

cout<<"\nThe names you entered are: ";

for(int i=0; i<number; i++) cout<<"\n"<<names[i];
1
2
3
4
5
int number;
cout<<"Enter number of names: ";
cin>>number;

string names[number];
Won't work.
Use a vector: vector<string> names(number);
i tested it and it worked perfectly :))
If it works your compiler is wrong
well i doubt that because I use the same compiler when i do my college exams and my professor uses it too. he has a doctors degree in programming and computer science, he should know which compiler is good or bad :)).. i'm using Mingw compiler..on which compiler\s my code doesn't compile?
G++ 4.3: error: ISO C++ forbids variable length array ‘names’
You may be compling with some non standard option.

From C++ Standards:
In a declaration T D where D has the form
D1 [ constant-expressionopt ]

'number' is a variable, not a constant expression
Error :

constant expression required in function main()


my guest is ==> names[number]
this is also my problem

hehehehe
and i cant use vector because im using tc++
and i cant use other compiler because this is the complier that we will use in showing that the program we made will run
Last edited on
you need to #include<vector>
If you can use std::string, you should be able of using std::vector
If you really can't use 'vector', use a dynamic array:
1
2
3
string*names = new string[number];
//use 'names'
delete[] names;
when i #include<vector> it gives me an error at school unable to open INCLUDE<VECTOR>
or some thing like that (sorry i forgot the exact sentence because im using another compiler here in my home) i know how to use vector but really it gives me an error in school
is their another way?
Last edited on
ahh tnx weeeeee it works tns bazzy!
where do i use" delet[] names;" in the statements and what its for? ( because it may become handy in future programs)
Last edited on
You can see this tutorial for a long explanation of new and delete: http://www.cplusplus.com/doc/tutorial/dynamic/
A short explanation is: with string*names = new string[number]; you are allocating an array of strings on the heap,
delete[] names; is releasing that part of memory. You should call it when you no longer need 'names'
Topic archived. No new replies allowed.