Hello everyone.
First, i'd like to introduce myself, would be very rude to just jump in here :)
I'm Bojan, student, 1. year, Croatia, and in this semester i've got a class "Programming 1". Here we're doing C++ (in Visual C++). And the idea of the class is to make our assignments, upload them, then explain in person. In total, i've had 5 assignments, and no.6 is in place.
So far, i've been doing these well, not much problems, but this one gives me a few questions, which i'd like to learn, and overcome them in future. I've found this great community on google :P and seen amazing things here. As noted, i'm not a "Do my homework for me" person, instead i will rather see mistakes that i make and with your help make errors go away.
The assignment i have is following (actually, there's 5 of them, but 1 gives me problems so far):
Enter 10 chars, then replace char with a number (a=1, b=2, d=4, etc.) and then sort them "alphabetically" :)
My idea is to enter 10 chars into an array, size [11], then do a while loop for each char and replace each char with appropriate number with a "switch" inside the while loop.
It goes well for first 9 letters, but when it comes to switch 1 char with a 2-digit number, i get problems. Also, another idea is to take the first array (10 chars which we input), then convert each letter in a second variable. In this case, i get problems sorting that 2nd array after converting chars.
So - if i can get some response for the following:
- is it possible to convert letters into numbers within same array?
- is it smart to use 2 arrays here?
- which is the best way to sort the numbers at the end - use <algorithm> with sort, or do some loop to sort them?
I could explain problems a bit more, but depends on what i need to specify more detail, i will :)
Here's the code so far: (it's within a function, so no basic stuff like <iostream>, etc., i'm using a menu with SWITCH within main, and call out this function)
char input[11];
int i;
cout << "Enter 10 letters: \n";
cin >> input;
sort (input, input+10);
char output[50];
i=0;
while (input[i]!='\0')
{
switch (input[i])
{
case 'a':
output[i]='1';
break;
case 'b':
output[i]='2';
break;
case 'c':
output[i]='3';
break;
case 'd':
output[i]='4';
break;
case 'e':
output[i]='5';
break;
case 'f':
output[i]='6';
break;
case 'g':
output[i]='7';
break;
case 'h':
output[i]='8';
break;
case 'i':
output[i]='9';
break;
case 'j':
output[i]='10';
break;
// etc, for entire alphabet
}
i++;
}
cout << output;
To conclude this post, sorry for a long intro, and a really big thanks to every feedback, i will be here from now on, i like the place so far.
Thanks, Bojan