Creating a program to output a text string.

I'd like to write a console program that will allow me to select options (1, 2, 3), then select another set of options (1, 2, 3). Then output a concatenated string of the two selected to a DOS prompt.

Where I am struggling is how to store the values/options in the C++ application, do I store it as an array? struct?

Example of what I want stored
First set of options
1) Desktop
2) Laptop
3) Server

Second set of options
1) Keyboard
2) Mouse
3) Lightpen

Possible output combinations
desktop_keyboard
laptop_keyboard
server_mouse
server_lightpen

etc

Any help is appreciated, been banging my head against the desk for a few days now.
I don't see any reason, really to store the options at all. I would personally use a switch to determine the first set, and add the chosen value to an empty string, followed by an underscore, then have another switch to determine the second set, and add the chosen value to the end of the same string.
Ciphermagi,

Thank you very much. I was having a syntax error when I was trying to declare my string in my case statement, apparently I had to enclose it in {} and then it worked.

This line
{string sMachineType = "desktops_";
cout << sMachineType;}


This is the code below

int main()
{
int machinetypeinput;

cout<<"1. Desktops\n";
cout<<"2. Laptops\n";
cout<<"3. Servers\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>> machinetypeinput;
switch ( machinetypeinput ) {
case 1: // Note the colon, not a semicolon
//desktops1();
{string sMachineType = "desktops_";
cout << sMachineType;}
break;
I was thinking more like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
switch(machinetypeinput){
    case 1:
        sMachineType = "desktops_";
        break;
    ...
    ...
    default:
        cout << "Invalid selection." << endl
        break;
    }

cout << "1. Keyboard" << endl;
...
...
switch(inputDeviceType){
    case 1:
        sMachineType += "Keyboard";
        break;
    ...
    ...
    default:
        cout << "Invalid selection." << endl;
}
cout << sMachineType << endl;
The operator += doesn't seem to be appending the two strings together. Is there another operator for text?
Try sMachineType = sMachineType + "keyboard";
If it doesn't work, then you're probably missing a header file.
I believe you can use the function strcat( string1, string1 ) to concatenate two strings together. It's from the header file cstring.
There's no point going to c style strings on this, especially since that will require a lot of conversions.
+= doesnt work because it is meant for numbers
Well it's overloaded for std::strings also

http://www.cplusplus.com/reference/string/operator+/
i know i realized that after I wrote it
Thanks for the help guys.

The problem I am having now is getting my variables that I set in the case statements out into other parts of the program so I can COUT them.

Here is the source as it stands now

#include <iostream>
//#include <cstdio>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
int machinetypeinput;

cout<<"1. Desktops\n";
cout<<"2. Laptops\n";
cout<<"3. Servers\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>> machinetypeinput;
string sMachineType;
switch ( machinetypeinput ) {
case 1: // Note the colon, not a semicolon
//desktops1();
{string sMachineType = "desktops_";
cout << sMachineType;}
break;
case 2: // Note the colon, not a semicolon
//laptops1();
{string sMachineType = "laptops_";
cout << sMachineType;}
break;
case 3: // Note the colon, not a semicolon
//servers1();
{string sMachineType = "servers_";
cout << sMachineType;}
break;
case 4: // Note the colon, not a semicolon
cout<<"Exit!\n";
break;
default: // Note the colon, not a semicolon
cout<<"Error, bad input, quitting\n";
break;
}


// This will determine the accessory
int accessoryinput;

cout<<"1. Keyboard\n";
cout<<"2. Mouse\n";
cout<<"3. Lightpen\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>> accessoryinput;
string sAccessoryType;
switch ( accessoryinput ) {
case 1: // Note the colon, not a semicolon
//sMachineType += "Keyboard";
{string sAccessoryType = "keyboard_";
cout << sMachineType << sAccessoryType << endl;}
//cout << sMachineType += sAccessoryType;}
//sMachineType = sMachineType << "keyboardtest";
//cout << sMachineType << endl;
break;
/*
case 2: // Note the colon, not a semicolon
{string sAccessoryType = "mouse_";
cout << sAccessoryType;}
break;
case 3: // Note the colon, not a semicolon
{string sAccessoryType = "lightpen_";
cout << sAccessoryType;}
break;
case 4: // Note the colon, not a semicolon
cout<<"Exit!\n";
break;
*/
default: // Note the colon, not a semicolon
cout<<"Error, bad input, quitting\n";
break;
}
cout << "blah" << sMachineType << endl;
system("PAUSE");
//cout << sMachineType << sAccessoryType;
//cin.get();

}

Topic archived. No new replies allowed.