943,536 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 917
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
May 25th, 2009
0

Re: stuck with strings and there relation to a file.

Much better (compared to the other revisions)! But you still haven't fixed //< MARKER (2) from my previous post! cin will not simply accept an array of characters. You have got to use the getline statement for this as well. Replace that with the following instead:
C++ Syntax (Toggle Plain Text)
  1. cin.getline(strSearchData, 255, '\n');
// Behold the above [code=cplusplus] tags... its not very hard to use, see ArkM's post above

Also, you store the result of your string compare in a char . strcmp returns an int , so store it one. Otherwise, forget bothering to waste a whole variable to do this, and directly set it as the condition in your if-else construct, so that you'll get something like: if(strcmp(string1, string2) == 0){ ... }

> still trying to work out how or where in my code i could use bubble sort to search for a particular name in a file

Bubble sort isn't a searching algorithm. As the name implies(strongly), it is an algorithm for sorting an array's contents. If it is a searching technique that you are looking for, then try the Binary-Search algorithm. For more information on the binary-search algorithm, see this.

A sample algorithm you could follow to search for a name using B-Search:
C++ Syntax (Toggle Plain Text)
  1. 1. Load all first names into an array of strings.
  2. 2. Apply B-Search algorithm to search for a given name

> how to call a function that uses void

Which function? And what do you mean, "uses void"?
Last edited by amrith92; May 25th, 2009 at 3:40 pm.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 25th, 2009
0

Re: stuck with strings and there relation to a file.

just a genearal function that has void infront of it rather than

int. i was trying to use code tags but i used [code = c++] not [code =cplusplus] in furure i will do so thanks for the help everyone.

binary searching you say okay will look and try thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightsolar is offline Offline
19 posts
since May 2009
May 25th, 2009
0

Re: stuck with strings and there relation to a file.

Click to Expand / Collapse  Quote originally posted by amrith92 ...
Much better (compared to the other revisions)! But you still haven't fixed //< MARKER (2) from my previous post! cin will not simply accept an array of characters. You have got to use the getline statement for this as well. Replace that with the following instead:
C++ Syntax (Toggle Plain Text)
  1. cin.getline(strSearchData, 255, '\n');
cin >> can be used to enter a string (array of char) - it's perhaps the simplest input method to use. But, it is also the most dangerous, as it doesn't know when to stop storing to the array. And, it cannot get a multi-word input. getline( ) is the generally better and safer choice, but not the only chooice.

But, brightsolar still has the mistake of using an unitialized pointer as the destination of that input action - a point I made in my earlier post.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
May 25th, 2009
0

Re: stuck with strings and there relation to a file.

> just a general function that has void infront of it rather than int.

Follow this example:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void MyFunc(int num)
  6. {
  7. cout << num;
  8. }
  9.  
  10. int AnotherFunc()
  11. {
  12. cout << "AnotherFunc Called!";
  13. return 0;
  14. }
  15.  
  16. int main()
  17. {
  18. cout << "A call to MyFunc : ";
  19.  
  20. MyFunc(10); // Prints "10" in console
  21. AnotherFunc(); // Prints "AnotherFunc Called!" in console
  22. cout << AnotherFunc(); // Prints "AnotherFunc Called!" and "0" to Console (Return value of function, AnotherFunc)
  23.  
  24. cout << MyFunc(25); // Invalid! Will cause an error! Function doesn't return anything to output to console!
  25.  
  26. int num = MyFunc(25); // Invalid!
  27. int nextnum = AnotherFunc(); // Valid! nextnum now holds 0
  28.  
  29. return 0;
  30. }

Infer the results of the above code, and you'll get the answer to your question. It also illustrates how you can use functions that return void as opposed to some other data type, and how not to use them.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 25th, 2009
0

Re: stuck with strings and there relation to a file.

> cin >> can be used to enter a string (array of char) - it's perhaps the simplest input method to use.

Whoops! Sorry for the wrong info I provided! I guess I forgot about this fact because I've been told (repeatedly) never to use this when I was learning the basics, and consequentially forgot that we can do this...
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 28th, 2009
0

Re: stuck with strings and there relation to a file.

thanks everyone project's not complete no searching but my lecturer wanted it so did not have time to play around more with searching will try now in spare time ty.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightsolar is offline Offline
19 posts
since May 2009

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: Linked List Copy Constructor
Next Thread in C++ Forum Timeline: any idea on this error?





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


Follow us on Twitter


© 2011 DaniWeb® LLC