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

Recommended Answers

All 7 Replies

ASCII code for 'a' is 97 what you can do is subtract 96 from each character. And typecast char to int. Instead of the switch statement try

while (input[i]!='\0')
{
output[i]=input[i]-96;
cout<<(int)output[i]<<endl;
i++;
}

to convert within the same array just change output to input. Of course you will need to convert the string into lowecase for this to work or you can handle uppercase letters seperately by subtracting 64 from them.

Thank you, this solution works beautiful.

The reason why i didn't go ASCII way, for i'm not too familiar with that area, but for now i get the concept.

Once again, thank you for your help

>>which is the best way to sort the numbers at the end - use <algorithm> with sort, or do some loop to sort them?

Depends on whether you need to write your own sort algorithm or not. If you instructor doesn't care then I'd use std::sort because its much simpler.

You don't have to convert the string to numbers before sorting. The string wlll sort without such a conversion. But I guess that's part of the assignment just to see how you can do things.

Of course this solution would fail if you entered any character will an ASCII value less than 96.

Of course this solution would fail if you entered any character will an ASCII value less than 96.

This assignment is currently being set just to accept letters, so this solution works for the required job :)

Hey everyone, again me, didn't want to open a new thread because i have another problem, regarding the topic name, so here it goes.

This time i have to enter data for 5 students: first name, last name and grade average into a struct, then sort it by average grade.

First of all, i'd like to get inputing the names correctly, but i get an error after inputing 5 sets of data.

void fourth_assignment()
{
	struct Students
	{
		char name[20];
		char last_name[20];
		float average;
	};

	Students input[5];
	int i;
	for (i=1; i<6; i++)
	{
		cout << "Enter name " << i << ". student: ";
		cin >> input[i].name;
		cout << "Enter last name " << i << ". student: ";
		cin >> input[i].last_name;
		cout << "Enter average grade " << i << ". student: ";
		cin >> input[i].average;
		cout << "--------------\n\n";
	}
	for (i=1; i<6; i++)
	{
		cout << setw (20) << input[i].name ;
		cout << setw (20) << input[i].last_name;
		cout << setw (5) << input[i].average<<endl;
	}


}

This is just a function which i call out in main, using switch menu.

I run the program without debugging (debugging gives me error at the last line - at }) and it comes up with an error:

Run-Time Check Failure #2 - Stack around the variable 'input' was corrupted.

So i would like to know where exactly is the problem located, as this is far i'm into structures, not too well with C++ generally.

In adition, which would be the best way to sort this entire structure by "average grade" - meaning i would need to swap the appropriate name and last name for following grade?

Thanks for any feedback, Bojan.

Found solution:

I declared one variable "input" too few, changing

Students input[5];

to 

Students input[6];

went well, the problem is gone now, my question just stand:

Which is the most simple way to sort them?
I found a way which includes working with vectors, which i'm not familiar yet. I just understand that i should STL sort (average, average+4), but then i have no idea how to swap the structure.

Thanks a bunch, Bojan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.