Hello Normal student,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
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.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
This is your code with comments and indenting to help you understand:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std; // <--- Best not to use.
std::string numerals = "MDCLVX"; // <--- Best to avoid a global variable like this. It will work just fine inside "main" and should be defined as a constant with "const".
int main()
{
int choice;
bool On = true;
while (On != false) // <--- Can be written as "while (On)" the same as saying "while (true)" or "while (1)".
// When it changes to false the while loop fails.
{
cout << endl;
cout << " 1 - Omvandla romersk siffra till arabisk siffra.\n";
cout << " 2 - Omvandla arabisk siffra till romersk siffra.\n";
cout << " 3 - Exit.\n";
cout << " Enter your choice and press return: ";
cin >> choice;
switch (choice)
{
case 1:
{
char romersk_nummer;
int nummer = 0;
cout << "Skriv in ett Romersk nummer (t.ex CCXIX) : ";
while (cin.get(romersk_nummer))
{
if (romersk_nummer == 'M')
nummer = nummer + 1000;
else if (romersk_nummer == 'D')
{
romersk_nummer = cin.peek();
if (numerals.find(romersk_nummer, 5) != std::string::npos)
{
nummer = nummer - 500;
continue;
}
else
{
nummer = nummer + 500;
continue;
}
}
else if (romersk_nummer == 'C')
{
romersk_nummer = cin.peek();
if (numerals.find(romersk_nummer, 4) != std::string::npos)
{
nummer = nummer - 100;
continue;
}
else
{
nummer = nummer + 100;
continue;
}
}
else if (romersk_nummer == 'L')
{
romersk_nummer = cin.peek();
if (numerals.find(romersk_nummer, 3) != std::string::npos)
{
nummer = nummer - 50;
continue;
}
else
{
nummer = nummer + 50;
continue;
}
}
else if (romersk_nummer == 'X')
{
romersk_nummer = cin.peek();
if (numerals.find(romersk_nummer, 2) != std::string::npos)
{
nummer = nummer - 10;
continue;
}
else
{
nummer = nummer + 10;
continue;
}
}
else if (romersk_nummer == 'V')
{
romersk_nummer = cin.peek();
if (numerals.find(romersk_nummer, 1) != std::string::npos)
{
nummer = nummer - 5;
continue;
}
else
{
nummer = nummer + 5;
continue;
}
}
else if (romersk_nummer == 'I')
{
romersk_nummer = cin.peek();
if (numerals.find(romersk_nummer) != std::string::npos)
{
nummer = nummer - 1;
continue;
}
else
{
nummer = nummer + 1;
continue;
}
}
else
{
cout << "Det du angett är ogiltigt";
}
cout << nummer << endl;
} // <--- Closing brace of case1 while loop.
break;
case 2:
int num;
cout << "Ange ett vanligt nummer: ";
cin >> num;
while (num > 0)
{
if (num >= 1000)
{
cout << "M";
num -= 1000;
}
else if (num >= 900)
{
cout << "CM";
num -= 900;
}
else if (num >= 500)
{
cout << "D";
num -= 500;
}
else if (num >= 400)
{
cout << "CD";
num -= 400;
}
else if (num >= 100)
{
cout << "C";
num -= 100;
}
else if (num >= 90)
{
cout << "XC";
num -= 90;
}
else if (num >= 50)
{
cout << "L";
num -= 50;
}
else if (num >= 40)
{
cout << "XL";
num -= 40;
}
else if (num >= 10)
{
cout << "X";
num -= 10;
}
else if (num >= 9)
{
cout << "IX";
num -= 9;
}
else if (num >= 5)
{
cout << "V";
num -= 5;
}
else if (num >= 4)
{
cout << "IV";
num -= 4;
}
else if (num >= 1)
{
cout << "I";
num -= 1;
}
} // <--- Closing brace of case 2 while loop.
break;
case 3:
cout << "End of Program.\n";
On = false;
break;
default:
cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
cin >> choice;
break;
} // <--- Closing brace of case 1.
} // <--- Closing brace of switch.
return 0;
} // <--- Closing brace of while loop.
} // <--- Closing brace of main.
|
Not only does indenting help, but a quick comment on the closing brace } before it gets to far away really helps.
I tend to indent the case statement to help readability.
In the above code the indentation helps give a better idea of where the opening and closing {}s fall. The closing brace of case 1 is not where it should be and case 2 has no block to deal with defining a variable inside a case statement.
I have also found the when defining a variable in a case statement it works best when the individual case statement is defined with a block as in:
1 2 3 4 5 6 7 8
|
switch(choice)
{
case 1:
{
int num{};
// other code.
} // End case 1 block.
|
Once you straighten out the {}s the program will compile properly.
So far I have only worked on getting the program to compile. I have not tried to run it yet.
Note for the future: When posting an error message do not just put "C2360" copy and paste the whole error message. Some know that "C2360" is a VS error code, but not everyone does. Also there are times I can load up the code and it will not produce the same error message or fail with the same problem that the poster of the question is having, so the problem you have needs to be explained and what you get for an error message is what everyone needs to see.
It also helps to mention the operating system and IDE you are using.
Hope that helps,
Andy