Assignment

I already done the menu part, but next few parts, i have no idea how to write.


#include <iostream>
using namespace std;
void Plaintocypher(int selection){
if (selection == 1){
cout << "1" << endl;
}
else{
cout << "2" << endl;
}
}
void Cyphertoplain(int selection){
if (selection == 1){
cout << "3" << endl;
}
else{
cout << "4" << endl;
}
}
void sort_data(int selection){
if (selection == 1){
cout << "5" << endl;
}
else{
cout << "6" << endl;
}
}
int main(){
int choice;
do{
cout << "1.Convert plain text to cypher text (SET 1)";
cout << "\n2.Convert cypher text to plain text (SET 1)";
cout << "\n3.Convert plain text to cypher text (SET 2)";
cout << "\n4.Convert cypher text to plain text (SET 2)";
cout << "\n5.Sort in ascending";
cout << "\n6.Sort in descending";
cout << "\n7.Quit" << endl;
cout << "\nEnter your choice: ";
cin >> choice;
switch(choice){
case 1:
Plaintocypher(1);
break;
case 2:
Cyphertoplain(1);
break;
case 3:
Plaintocypher(2);
break;
case 4:
Cyphertoplain(2);
break;
case 5:
sort_data(1);
break; //1 for ascending sort
case 6:
sort_data(2);
break; //2 for descending sort
case 7:
exit(0);
case 8:
cout << "Invalid option selected. Please try again";
break;
}
}while (choice!=7);
return 0;
}
Hello reneejong,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.


You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Overall the code looks OK, but I have not had the chance to compile it yet.

The menu can be written this way:
1
2
3
4
5
6
7
8
9
10
11
cout <<
    "\n 1. Convert plain text to cypher text (SET 1)"
    "\n 2. Convert cypher text to plain text (SET 1)"
    "\n 3. Convert plain text to cypher text (SET 2)"
    "\n 4. Convert cypher text to plain text (SET 2)"
    "\n 5. Sort in ascending"
    "\n 6. Sort in descending"
    "\n 7. Quit"
    "\n Enter your choice: ";

cin >> choice;

I have found it makes the code easier to read and work with. Plus it gives you a better idea of how it will look on the screen.

What works to your advantage is the "cin" statement will flush the "output buffer" before any input is allowed. This eliminates all the "endl" statements that you have. Also the "endl"s come with overhead for all it does. in a large program this may show a noticeable slow down that can be avoided.

In the switch there is no "case 8:". This should never be allowed. What you want is "default:" at the end of the switch.

For "case 7" do not use "exit".
jlb once wrote:

You really should try to avoid the C function exit(), it doesn't know about C++ classes and can cause data loss if used inappropriately. Since this is C++ start thinking about using exceptions when you need to "abort" the program.


Actually all you need for "case 7" is a break statement to leave the switch and since "choice" is 7 it will cause the while condition to fail and end the program normally.

Andy
What is a "SET 1"?

What is the difference between a SET 1 and a SET 2?

Why are you passing an int into your functions, seems to me that a string would probably be better.

What type of cypher are you trying to use?

Your switch() statement should really have a default clause to handle any input that is not within the desired range ( 1 - 7 ). Remember that you are using an int for the "choice" so you could have something like 9, 10, -1, 1000, -10000, etc. since these are all valid integers.

@ jlb,

My question is what is there to be sorted and how.

Andy
Hello reneejong,

I loaded the program into my IDE and it compiled with no errors or warnings with the changes I made.

The program works fine and all you need to do is add all the proper code to the functions.

Not sure how to advise at this point because yo have not posted any directions on what the program needs to do or how to encode and decode the string.

What I can say is that you will need the header file "string" to work with.

If the program needs to read anything from a file you will also need "fstream" and the input file, or a good sample to work with and if you know what the output should be based on a given input that helps.

If this is an assignment for school, as I believe it is, post the full instructions that you were given. It helps others know what you need to do.

Since I had some time I came up with this for the menu:
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
do
{
    cout <<
        "\n 1. Convert plain text to cypher text (SET 1)"
        "\n 2. Convert cypher text to plain text (SET 1)"
        "\n 3. Convert plain text to cypher text (SET 2)"
        "\n 4. Convert cypher text to plain text (SET 2)"
        "\n 5. Sort in ascending"
        "\n 6. Sort in descending"
        "\n 7. Quit"
        "\n Enter your choice: ";

    cin >> choice;

    if (!std::cin || (choice < 1 || choice > 7))
    {
        if (!std::cin)
        {
            std::cout << "\n     Invalid entry! Must be a number.\n";

            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
        }
        else if (choice < 1 || choice > 7)
        {
            std::cout << "\n     Invalid choice! Must be 1 - 7.\n";
        }
    }
} while (choice < 1 || choice > 7);

Usually I put this in a function that returns a valid choice. Then I eventually put the function in a file that I can include in the solution. Then adjust the cout statement as needed.

This will eliminate the need for "case 8", which is never used, and the "default" case.

While I am waiting I also changes the "cout" statements in the functions:
1
2
3
4
5
6
7
8
9
10
11
void Plaintocypher(int selection)
{
    if (selection == 1)
    {
        cout << "\n    In function \"Plaintocyphe\" 1\n";  // <--- Changed.
    }
    else
    {
        cout << "\n    In function \"Plaintocyphe\" 2\n";  // <--- Changed.
    }
}


Andy
@Andy My question is what is there to be sorted and how.


Probably:

Why are you passing an int into your functions, seems to me that a string would probably be better.


Also this comes into play as well:

What type of cypher are you trying to use?


This is important also:
What is a "SET 1"?

What is the difference between a SET 1 and a SET 2?


The OP seems to be throwing code at a wall to see what sticks. There is not enough information provided to do anything other than guess as to the requirements.

Reneejong,

You need to understand that we aren't in your class. We aren't in your school, we don't know your professor or what class you're taking. We're just a bunch of random C++ programmers from around the world who enjoy helping others learn the language.

That's why we're asking questions that may seem obvious to you, like "what are SET 1 and SET 2?" and "what is the encryption algorithm" and "please post the assignment." Obviously these would be stupid questions from someone in your class, but with us, it's more like you stopped a stranger on the street and said "what's wrong with my code?" Except that we happen to understand the programming language.
Topic archived. No new replies allowed.