User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 423,509 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,687 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 973 | Replies: 10
Reply
Join Date: Jul 2007
Posts: 6
Reputation: rainbow78 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rainbow78 rainbow78 is offline Offline
Newbie Poster

array/funtion problem, please help

  #1  
Jul 30th, 2007
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

  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 9:25 pm. Reason: add line numbers to code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,123
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 929
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: array/funtion problem, please help

  #2  
Jul 30th, 2007
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:
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 9:31 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jul 2007
Location: Florida
Posts: 19
Reputation: xander85 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
xander85's Avatar
xander85 xander85 is offline Offline
Newbie Poster

Re: array/funtion problem, please help

  #3  
Jul 30th, 2007
In the dataEntry() method after the cin >> there is no variable to store the users input!!! don't you see that?!?!? :O
Reply With Quote  
Join Date: Jul 2007
Location: Florida
Posts: 19
Reputation: xander85 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
xander85's Avatar
xander85 xander85 is offline Offline
Newbie Poster

Re: array/funtion problem, please help

  #4  
Jul 30th, 2007
lol, in main wth is for { }

there is no condition...

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

Have you been studying???
Reply With Quote  
Join Date: Jul 2007
Location: Bristol, England
Posts: 8
Reputation: spider_pig is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
spider_pig spider_pig is offline Offline
Newbie Poster

Re: array/funtion problem, please help

  #5  
Jul 30th, 2007
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;
Reply With Quote  
Join Date: Jul 2007
Posts: 6
Reputation: rainbow78 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rainbow78 rainbow78 is offline Offline
Newbie Poster

Re: array/funtion problem, please help

  #6  
Jul 30th, 2007
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.
Reply With Quote  
Join Date: Jul 2007
Location: Florida
Posts: 19
Reputation: xander85 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
xander85's Avatar
xander85 xander85 is offline Offline
Newbie Poster

Re: array/funtion problem, please help

  #7  
Jul 31st, 2007
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;
Reply With Quote  
Join Date: Jul 2007
Location: Florida
Posts: 19
Reputation: xander85 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
xander85's Avatar
xander85 xander85 is offline Offline
Newbie Poster

Re: array/funtion problem, please help

  #8  
Jul 31st, 2007
You say the book has no example? What book you have???
Reply With Quote  
Join Date: Jul 2007
Posts: 6
Reputation: rainbow78 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rainbow78 rainbow78 is offline Offline
Newbie Poster

Re: array/funtion problem, please help

  #9  
Jul 31st, 2007
I don't care for rudeness, if you would be so kind not to post in this thread again I would appreciate it
Reply With Quote  
Join Date: Jul 2005
Posts: 1,142
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 150
Lerner Lerner is offline Offline
Veteran Poster

Re: array/funtion problem, please help

  #10  
Jul 31st, 2007
>>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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:29 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC