strcmp sort with incorrect output

The point of this exercise that I need to complete is to use strcmp to do a sort. In my learning, we have not reached sort() or any other sorting functions. It requires to use strcmp(). Plus we have not reached loops yet, that is next chapter. I saw some old posts that were closed, but did not see an answer I could use, so, here I am, so sorry if I posted wrong. Using code::blocks 12.11 with GNU GCC compiler. So now my question...

ok, this finally complies and lets me enter names, however the output is all funny unicode characters instead of the sorted names. Now, I am learning, so if in commenting, I have my logic wrong too, LOL, sorry. But I did my own little IPO chart and on paper I got it. Anyways, could someone point me to why it is not outputting the entered names correctly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    const int SIZE = 10;
    char name[SIZE], low[SIZE], med[SIZE], high[SIZE], temp[SIZE];
    // Get input
    cout << "Enter name 1.\n";
    cin >> name; 
    low[SIZE] = name[SIZE];     // name1 in low to start
    cout << "Enter name 2.\n";
    cin >> name;                // Get name2
    if (strcmp(low, name) < 0)  // if low less than name, leave low alone
    {                           // Put name2 in med
        med[SIZE] = name[SIZE]; 
    }
    else if (strcmp(low, name) > 0)  // If low is larger, the two
    {                                // need switched.
        med[SIZE] = low[SIZE];
        low[SIZE] = name[SIZE];
    }
    cout << "Enter name 3.\n";       // enter name 3
    cin >> name;
    if (strcmp(med, name) < 0)       // if med is smaller, then set
                                     // name3 to high and finish.
    {
        high[SIZE] = name[SIZE];
    }
    else if (strcmp(med, name) > 0)  // if med is higher, then move
                                     // name from med to high,
    {                                // then put the name in the med variable.
        high[SIZE] = med[SIZE];
        med[SIZE] = name[SIZE];
    }
    if (strcmp(low, med) > 0)        // lastly, Need to make sure low
                                     // is not larger than med.
                                     // if low is now larger than med,
                                     // they need to be switched.
    {
        temp[SIZE] = low[SIZE];
        low[SIZE] = med[SIZE];
        med[SIZE] = temp[SIZE];
    }

    cout << "The ordered names are: " << endl << endl;
    cout << low;
    cout << med;
    cout << high;

    return 0;
}
Instead of the statement

low[SIZE] = name[SIZE]; // name1 in low to start

and similar statements you should write

strcpy( low, name );
Thanks Vlad, I'll have to edit my original post, because you came up with one that I still can't use yet with strcpy(). Don't get me wrong, I would be glad to, but that function is in a later chapter and I have to be able to use just ole fashioned assignment from one variable to another from what I understand. So I have to use basics that you see above. So, you know, I have to learn basics before hard math... type concepts.

Again, thanks though.
If you can't use strcpy, then you will have to write a loop to copy the string one character at a time until you reach the null terminating byte. Actually, that's a rather bigger task than using strcpy, though the principle is straightforward.

strcpy() and strcmp() are both functions contained in <cstring>.
Normally I would expect people to learn strcpy() first, before learning about strcmp().
Last edited on
LOL Chervil, talk to the author and publisher! Well plus you missed where I mentioned that we haven't got to loops yet to where I have to do it all basic style. It requires that I get three names, then use strcmp to sort them, and strcpy is a later chapter so I can't use it yet, nor loops.
Hmmm, I'm beginning to wonder if there is an error in the book and it can't be done. Ugg, and the teacher is there, but my last resort. Oh well.
What book are you studying?
I'm reading C++ Primer, And there is a site where you can download Errata.
LOL:
you can hard code the copy operation, but i prefer to watch the paint drying before thinking of this solution.

edit:
if you can define a function and use it, recursion isn't considered more than a function call.
Last edited on
Pebble, it's Starting out with C++ From Control Structures through Objects. It's for school, so no option for other book, LOL. And I'm up to chapter 4 with one of it's end chapter problems. Basically asks for user to enter 3 names, assume none are the same, and display them sorted. Chapter 1 is intro, 2 and 3 are basic cin and cout stuff. Chapter 4 is if else if and switch type decisions. And strcmp is there. So, somehow I'm suppose to be able to do it.

But yes, I will do that, and see if there is something I should know, Great Point!

Rechard3, Yeah, I was getting that same hard code option from other sources too, but yeah, since we haven't hit loops, functions, or any or the stuff you and others are mentioning, I have the feeling something is out of place somewhere. Now, that is usually me, but I still want to know where I flubbed..> LOL.
@londel26
Now, that is usually me, but I still want to know where I flubbed..> LOL.


i don't really think it's you.

imagine we have a poor villager.
now imagine we stripped him naked, and give him white T-shirt and a pair of shorts.
imagine we gave him a spoon.
and we told him to go slay a dragon with that spoon, and no armor at all.

that my friend is your situation, you can't use anything, and expected to complete this task with no tools.
i know creativity is creating something beauty with few tools, but definetly not like that....lol
LOL, no problem, thank you for the help. It's much appreciated.
Topic archived. No new replies allowed.