| | |
stuck with strings and there relation to a file.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Much better (compared to the other revisions)! But you still haven't fixed
// 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
> 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:
> how to call a function that uses void
Which function? And what do you mean, "uses void"?
//< 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)
cin.getline(strSearchData, 255, '\n');
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. Load all first names into an array of strings. 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."
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."
•
•
•
•
Much better (compared to the other revisions)! But you still haven't fixed//< MARKER (2)from my previous post!cinwill not simply accept an array of characters. You have got to use thegetlinestatement for this as well. Replace that with the following instead:
C++ Syntax (Toggle Plain Text)
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.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
> just a general function that has void infront of it rather than int.
Follow this example:
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
Follow this example:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void MyFunc(int num) { cout << num; } int AnotherFunc() { cout << "AnotherFunc Called!"; return 0; } int main() { cout << "A call to MyFunc : "; MyFunc(10); // Prints "10" in console AnotherFunc(); // Prints "AnotherFunc Called!" in console cout << AnotherFunc(); // Prints "AnotherFunc Called!" and "0" to Console (Return value of function, AnotherFunc) cout << MyFunc(25); // Invalid! Will cause an error! Function doesn't return anything to output to console! int num = MyFunc(25); // Invalid! int nextnum = AnotherFunc(); // Valid! nextnum now holds 0 return 0; }
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."
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."
>
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...
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."
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."
![]() |
Similar Threads
- I got stuck on loading the matrix from file (C++)
- Binary File IO (C#)
- Exporting multiple Strings to a text file? (Java)
- Edit and replace strings in a text file (C++)
- File handling: How to search and replace strings in file using user input (C)
- Read from a file then placing strings into a 2d array (C)
- Help with comparing user input to a text file! (C++)
- Read and writing strings from structures (C++)
Other Threads in the C++ Forum
- Previous Thread: Linked List Copy Constructor
- Next Thread: any idea on this error?
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






