Please support our C++ advertiser: Programming Forums
Views: 1038 | Replies: 10
![]() |
•
•
Join Date: Jul 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
here what I got and I keep getting a error because of the "string names[];"
these were my guide lines and I'm getting confused
//declare an array of datatype string
//call it names[]
//declare a function dataEntry that takes a string array as input
void dataEntry(string arr[]);
//implement the function: let the user enter names
//declare a function print that takes a string array as input
void print(string arr[]);
//implement the function; use a for loop to print arr[]
//call your function in main() tp print the array
print(names);
thanks for helping me out
these were my guide lines and I'm getting confused
//declare an array of datatype string
//call it names[]
//declare a function dataEntry that takes a string array as input
void dataEntry(string arr[]);
//implement the function: let the user enter names
//declare a function print that takes a string array as input
void print(string arr[]);
//implement the function; use a for loop to print arr[]
//call your function in main() tp print the array
print(names);
thanks for helping me out
cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void dataEntry(string arr[]); void print(string arr[]); string x; int main() { string names[]; for { arr[]; } int blank; cin >>blank; return 0; } void dataEntry(string arr[]) { cout<< "Please enter a name" cin>>; } void print(string arr[]) { }
Last edited by Ancient Dragon : Jul 30th, 2007 at 10:25 pm. Reason: add line numbers to code tags
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,666
Reputation:
Rep Power: 40
Solved Threads: 990
line 11: when declaring the array you have to tell it how many items the array will contain. For example, if you want the array to contain not more than 10 names then declare it like this:
Declaring dynamic arrays is also possible, but apparently your class is not yet at that level, so I'll just ignore that possiblility for now.
lines 12 thru 15: not needed in function main(). The instructions you posted say nothing about doing that. You do need to add a loop in function print() to display each of the names.
Suggestion: don't attempt to do all the instructions at one time. Instead, code and test them one instruction at a time so that you don't get so confused.
string names[10];
Declaring dynamic arrays is also possible, but apparently your class is not yet at that level, so I'll just ignore that possiblility for now.
lines 12 thru 15: not needed in function main(). The instructions you posted say nothing about doing that. You do need to add a loop in function print() to display each of the names.
Suggestion: don't attempt to do all the instructions at one time. Instead, code and test them one instruction at a time so that you don't get so confused.
Last edited by Ancient Dragon : Jul 30th, 2007 at 10:31 pm.
•
•
Join Date: Jul 2007
Location: Bristol, England
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 1
Your code is some way short of a working solution.
I don't think you need a for loop in the main function, probably
a loop is needed in the data entry function. It is not clear whether
there is a set number of names to be entered or if the array is to be created dynamically as suggested in a previous post. If so then a while loop may be needed here eg
while (More)
{
cout << "Enter next name: ";
cin >> InString;
I don't think you need a for loop in the main function, probably
a loop is needed in the data entry function. It is not clear whether
there is a set number of names to be entered or if the array is to be created dynamically as suggested in a previous post. If so then a while loop may be needed here eg
while (More)
{
cout << "Enter next name: ";
cin >> InString;
•
•
Join Date: Jul 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
I haven't put anything into cin because I don't know what to do. What would the InString stand for?
I'm really getting lost, when making the loop to have the user put in the 10 names, you would do something like
for (int i=0; i<11; i++)
but what would be the cin; what it be another string (string x
and how would you get it inside the array? Every thing I have tried enter doesn't work or just brings up a blank screen that doesnt loop. any help?
how does it a sign the the names down the list inside the array.
My bigest problem is that the book doesn't give many ex and well most of them are just cout and not cout/cin.
I'm really getting lost, when making the loop to have the user put in the 10 names, you would do something like
for (int i=0; i<11; i++)
but what would be the cin; what it be another string (string x
and how would you get it inside the array? Every thing I have tried enter doesn't work or just brings up a blank screen that doesnt loop. any help?how does it a sign the the names down the list inside the array.
My bigest problem is that the book doesn't give many ex and well most of them are just cout and not cout/cin.
omg what school you going to?
inString would be a varible that you would declare somewhere b4 you use it in the:
cin>> inString //statement
i.e.
string inString; //Now memory is allocated for the user to input a string of characters, not including spaces, (you'll learn how to handle white spaces later, with a simple function)....
//Now you can use the memory to store the users input...
i.e.
cin >> inString;
inString would be a varible that you would declare somewhere b4 you use it in the:
cin>> inString //statement
i.e.
string inString; //Now memory is allocated for the user to input a string of characters, not including spaces, (you'll learn how to handle white spaces later, with a simple function)....
//Now you can use the memory to store the users input...
i.e.
cin >> inString;
•
•
Join Date: Jul 2005
Posts: 1,353
Reputation:
Rep Power: 9
Solved Threads: 182
>>I'm really getting lost, when making the loop to have the user put in the 10 names, you would do something like
for (int i=0; i<11; i++)
Not quite. Use your fingers to count starting at, and including, zero and going through, and including 10. How many numbers did you count?
How many names do you want to store? What could the syntax be to get that number of names?
>>but what would be the cin;
If you declare arr like this:
string arr[10];
Then arr can hold up to 10 strings. Each string in arr is accessable by using the array name followed by the [] operator with an index inside the []s. The indexes indicate how many strings from the first string do you need to go to find the desired string. So if the index is zero, you want the first string. If the index is 1 you want one string beyond the first one, or the second string in the array. If the index is 5 then you wan the fifth string from the first, or the sixth string in the array.
Each string in arr is therefore known as
arr[x]
where x is the index.
You can display arr[x] using cout and you can assign a value to arr[x] using any valid techique; such as cin >>, the assignment operator, etc. In other words, arr[x] is a string, as long as x is a valid index. Valid indexes will be from zero to size (or capacity depending on your syntax) of the array minus 1. So if arr has size 10 then valid indexes will be from zero to 9.
for (int i=0; i<11; i++)
Not quite. Use your fingers to count starting at, and including, zero and going through, and including 10. How many numbers did you count?
How many names do you want to store? What could the syntax be to get that number of names?
>>but what would be the cin;
If you declare arr like this:
string arr[10];
Then arr can hold up to 10 strings. Each string in arr is accessable by using the array name followed by the [] operator with an index inside the []s. The indexes indicate how many strings from the first string do you need to go to find the desired string. So if the index is zero, you want the first string. If the index is 1 you want one string beyond the first one, or the second string in the array. If the index is 5 then you wan the fifth string from the first, or the sixth string in the array.
Each string in arr is therefore known as
arr[x]
where x is the index.
You can display arr[x] using cout and you can assign a value to arr[x] using any valid techique; such as cin >>, the assignment operator, etc. In other words, arr[x] is a string, as long as x is a valid index. Valid indexes will be from zero to size (or capacity depending on your syntax) of the array minus 1. So if arr has size 10 then valid indexes will be from zero to 9.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode