Hi All,

Please keep the laughing until the end of this request.

The new assignment that I have to work on, is a string sorter. I am to ask the user to enter how many names does he/she need to sort, should be a number between 2 and 25. Then sort these names alphabetically in the order the user chooses.

The code is written for all of the above.

My question is:

NOT USING ARRAYS, because it is not yet covered in class how can I use an elegant For-Next loop to enter all these names?

So for example:

How many names do you want to sort: user enters 5, gets stored in variable iHowManyNames

Then you have:

For (iCounter = 1; iCounter <= iHowManyNames; iCounter++)
[tab]cout << "Enter name " << iCounter << " of " << iHowManyNames << " :";
[tab]cin.getline (cName, 20);

Now obviously this will not work, because all the names will be entered into a single variable, cName and thus I will capture only the last one entered. But my question is about an elegant alternative to:

cin.getline (cName1, 20);
cin.getline (cName2, 20);
cin.getline (cName3, 20);
cin.getline (cName4, 20);
cin.getline (cName5, 20);
...
cin.getline(cName25,20);

Thanks for any idea

Wassim

Recommended Answers

All 8 Replies

Member Avatar for iamthwee

No, you must use arrays to sort.

>NOT USING ARRAYS
If you can't use arrays because they haven't been covered, you can't use any of the alternatives, because they're more advanced. Your program is likely impossible with the given restrictions, sorry.

>NOT USING ARRAYS
If you can't use arrays because they haven't been covered, you can't use any of the alternatives, because they're more advanced. Your program is likely impossible with the given restrictions, sorry.

Thanks Narue

Could you please elaborate about the more advanced alternatives you mention.

Right now, I am toying with the idea of writing the variables to a file using ofstream in the body of the For-Next loop, and then use another Lopp to read them back using ifstream and sort them.

What else can I do?

Cordially

Wassim

Member Avatar for iamthwee

All your suggestions are pointless ways to avoid using arrays.

If that's what your school recommends you do - then I would find another school.

Chances are you've misread your assignment requirements or skipped the class when you covered arrays :S

>I am toying with the idea of writing the variables to a file using ofstream
You've covered file I/O and not arrays?

>read them back using ifstream and sort them
It's easy to say "and sort them", but how do you plan on doing that?

Maybe you should explain what you've already learned so that we don't have to guess what's allowed and what's not.

>I am toying with the idea of writing the variables to a file using ofstream
You've covered file I/O and not arrays?

Yes. The book we are using is custom made for our class. Chapters are not in the order one expects, and on more than one previous occasions, the homework was modified because of that. Things do seem out of order, but... We did cover file I/O but very basic information was given. I know how to open a file, write to it, and read from it, and close it

>It's easy to say "and sort them", but how do you plan on doing that?

I know about strcmp() and I will use a set of If statements to swap the names based on the outcome of strcmp(). I know this is going to take a lot of Ifs but again, this is what I know so far.

>Maybe you should explain what you've already learned so that we don't have to guess what's allowed and what's not.

Well we have just finished chapter 5 looping. We went through all kinds of Loops. While loops, Do-While loops, and For-Next loops. We also finished If else decision making, Switch statement, logical operators, and chapter 1 and 2 were about C++ syntax and structure.

Arrays come in chapter 7, and chapter 6 is about functions. We will start chapter 6 on Saturday.

I do have previous programming experience, but not in C++.

Thanks

Wassim

You have got 25 variables and now use modified bubble sort , e.g if cname1 > cname 2 swap them .

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.