stuck with strings and there relation to a file.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 152
Reputation: amrith92 is on a distinguished road 
Solved Threads: 15
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

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

 
0
  #11
May 25th, 2009
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:
  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:
  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.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 16
Reputation: brightsolar is an unknown quantity at this point 
Solved Threads: 0
brightsolar brightsolar is offline Offline
Newbie Poster

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

 
0
  #12
May 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,677
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

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

 
0
  #13
May 25th, 2009
Originally Posted by amrith92 View Post
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:
  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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 152
Reputation: amrith92 is on a distinguished road 
Solved Threads: 15
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

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

 
0
  #14
May 25th, 2009
> just a general function that has void infront of it rather than int.

Follow this example:

  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.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 152
Reputation: amrith92 is on a distinguished road 
Solved Threads: 15
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

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

 
0
  #15
May 25th, 2009
> 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...
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 16
Reputation: brightsolar is an unknown quantity at this point 
Solved Threads: 0
brightsolar brightsolar is offline Offline
Newbie Poster

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

 
0
  #16
May 28th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC