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?
#include <iostream>
#include <cstring>
usingnamespace std;
int main()
{
constint 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];
}
elseif (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];
}
elseif (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;
}
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.
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().
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.
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.
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