944,117 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1832
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 30th, 2007
0

array/funtion problem, please help

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6. void dataEntry(string arr[]);
  7. void print(string arr[]);
  8. string x;
  9. int main()
  10. {
  11. string names[];
  12. for
  13. {
  14. arr[];
  15. }
  16. int blank;
  17. cin >>blank;
  18. return 0;
  19. }
  20. void dataEntry(string arr[])
  21. {
  22. cout<< "Please enter a name"
  23. cin>>;
  24.  
  25. }
  26. void print(string arr[])
  27. {
  28.  
  29. }
Last edited by Ancient Dragon; Jul 30th, 2007 at 10:25 pm. Reason: add line numbers to code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rainbow78 is offline Offline
6 posts
since Jul 2007
Jul 30th, 2007
0

Re: array/funtion problem, please help

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:
C++ Syntax (Toggle Plain Text)
  1. 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,961 posts
since Aug 2005
Jul 30th, 2007
0

Re: array/funtion problem, please help

In the dataEntry() method after the cin >> there is no variable to store the users input!!! don't you see that?!?!? :O
Reputation Points: 16
Solved Threads: 1
Light Poster
xander85 is offline Offline
29 posts
since Jul 2007
Jul 30th, 2007
0

Re: array/funtion problem, please help

lol, in main wth is for { }

there is no condition...

for(int i = 0, i < //?//, i++)
{
}

Have you been studying???
Reputation Points: 16
Solved Threads: 1
Light Poster
xander85 is offline Offline
29 posts
since Jul 2007
Jul 31st, 2007
0

Re: array/funtion problem, please help

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;
Reputation Points: 10
Solved Threads: 1
Newbie Poster
spider_pig is offline Offline
8 posts
since Jul 2007
Jul 31st, 2007
0

Re: array/funtion problem, please help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rainbow78 is offline Offline
6 posts
since Jul 2007
Jul 31st, 2007
0

Re: array/funtion problem, please help

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;
Reputation Points: 16
Solved Threads: 1
Light Poster
xander85 is offline Offline
29 posts
since Jul 2007
Jul 31st, 2007
0

Re: array/funtion problem, please help

You say the book has no example? What book you have???
Reputation Points: 16
Solved Threads: 1
Light Poster
xander85 is offline Offline
29 posts
since Jul 2007
Jul 31st, 2007
0

Re: array/funtion problem, please help

I don't care for rudeness, if you would be so kind not to post in this thread again I would appreciate it
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rainbow78 is offline Offline
6 posts
since Jul 2007
Jul 31st, 2007
0

Re: array/funtion problem, please help

>>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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: serious help calculating averages
Next Thread in C++ Forum Timeline: How to Compare two SYSTEMDATE?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC