I was trying to make a program to convert Roman numerals to arabic numeral, but for some reason when he gets to the function setNumeri, the input text gets messed up super badly even before it can even get to the conversion part.
The problem is the function setNumeri, you can ignore everything else, because I pasted everything to be sure not to leave anything out.
I apologize for the fact that some variables are in Italian, but it should be pretty straightforward.
Here's the code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class IX{
public:
void setNumeri(string rom);
void setArabo();
int getArabo();
private:
int length;
int Arabo;
int Numeri[];
};
void IX::setNumeri(string rom){
length = rom.length();
for(int i = length - 1; i >= 0; i--) {
cout << rom.at(i) << endl;
switch (rom.at(i)){
case 'I':
Numeri[i] = 1;
break;
case 'V':
Numeri[i] = 5;
break;
case 'X':
Numeri[i] = 10;
break;
case 'L':
Numeri[i] = 50;
break;
case 'C':
Numeri[i] = 100;
break;
case 'D':
Numeri[i] = 500;
break;
case 'M':
Numeri[i] = 1000;
break;
}
}
}
void IX::setArabo(){
int counter = Numeri[length];
for(int i = length - 1; i >= 0; i--){
if(Numeri[i] == 0) {
counter = counter + Numeri[i];
} else if(Numeri[i] > Numeri[i-1]) {
counter = counter - Numeri[i-1];
i--;
} else if(Numeri[i] <= Numeri[i-1]) {
counter = counter + Numeri[i-1];
}
}
Arabo = counter;
}
int IX::getArabo(){
return Arabo;
}
int main(){
IX lol;
string hellothere;
cout << "Roman numeral:" << endl << endl;
cin >> hellothere;
lol.setNumeri(hellothere);
cout << lol.getArabo() << endl;
return 0;
}
When I input XIII, the output is:
I
I
□
-107362937
That last ominous number is the result of the conversion, while the first 3 characters (one is missing in action, and one is not recognised at all) are the output by string.at() that, as you can see, did a wonderful job getting the string characters right. Tried using string[] instead but no success.
What's even weirder is the fact that once I deleted the whole switch case string.at() gets the string right, it looks like the string gets somehow messed up during the switch part. The same happens using an if statement instead of the switch.
Thanks in advance.
try following code it is working perfectly fine.
#include <cstdlib>
#include <string>
using namespace std;
class IX{
public:
void setNumeri(string rom);
int getArabo();
private:
int length;
int Numeri[100];
};
void IX::setNumeri(string rom){
length = rom.length();
for (int i = 0; i < 100; i++)
{
Numeri[i]=-1;
}
for(int i = length - 1; i >= 0; i--) {
cout << rom.at(i) << endl;
switch (rom.at(i)){
case 'I':
Numeri[i] = 1;
break;
case 'V':
Numeri[i] = 5;
break;
case 'X':
Numeri[i] = 10;
break;
case 'L':
Numeri[i] = 50;
break;
case 'C':
Numeri[i] = 100;
break;
case 'D':
Numeri[i] = 500;
break;
case 'M':
Numeri[i] = 1000;
break;
}
}
}
int IX::getArabo(){
int sum=0;
for (int i = 0; i < 100; i++)
{
if (Numeri[i]==-1)
{
return sum;
}
else
{
sum+=Numeri[i];
}
}
}
int main(){
IX lol;
string hellothere;
cout << "Roman numeral:" << endl << endl;
cin >> hellothere;
lol.setNumeri(hellothere);
cout << lol.getArabo() << endl;
return 0;
}
Related
I have a program that should translate values from one number system to another, but I have a problem with "_itoa_s" writes that [Error] '_itoa_s' was not declared in this scope I tried to connect libraries <cstdlib> and <stdlib.h> I also tried replacing itoa with "snprintf" but it does not help in the compiler there are even more errors, please help me fix the error,
Here is the code:
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
setlocale(LC_ALL, "rus");
int in, iz, k, s = 0, p;
char str[255];
cout << "Enter the number system from which you want to translate" << endl;
cin >> iz;
cout << "Enter the number system to which we will translate" << endl;
cin >> in;
cout << "Enter the number" << endl;
cin >> str;
p = strlen(str) - 1;
for (int i = 0; i < str[i]; i++)
{
switch (str[i])
{
case 'a': k = 10; break;
case 'b': k = 11; break;
case 'c': k = 12; break;
case 'd': k = 13; break;
case 'e': k = 14; break;
case 'f': k = 15; break;
case '1': k = 1; break;
case '2': k = 2; break;
case '3': k = 3; break;
case '4': k = 4; break;
case '5': k = 5; break;
case '6': k = 6; break;
case '7': k = 7; break;
case '8': k = 8; break;
case '9': k = 9; break;
case '0': k = 0; break;
}
s = s + k * pow(iz, p);
p--;
}
char result[255];
_itoa_s(s, result, in);
cout << "The result of a translation from a radix " << iz << " to radix " << in << " = " << result;
return 0;
}
Here's an alternative that doesn't involve switch, pow or itoa:
// Notice, the index is the value of the digit.
const std::string digits[] = "0123456789abcdef";
//...
const size_t length = str.len;
// Note: iz is the radix for "input" conversion
int number = 0;
for (unsigned int i = 0; i < length; ++i)
{
const std::string::size_type position = digits.find(str[i]);
number = number * iz; // Shift the number by one digit.
number += position;
}
Notice: error handling, such as invalid digits, is left as an exercise for the OP. Invalid digits are not restricted to characters outside the set, but also digits whose index is greater than the conversion radix.
For ultimate understanding, single step through the code with pen and paper. :-)
You can use the table for converting to the target radix, in (I'd rather use a more descriptive variable name).
std::string translated_number;
while (number > 0)
{
const unsigned int index = number % output_radix; // Output_radix == 'in'
const char digit_character = digits[index];
translated_number.insert(0, 1, digit_character);
number = number / output_radix;
}
I should make a program where I should print out the short (compressed word), having the long one. For an example: 8S2Q3R is short for SSSSSSSSQQRRR. Now, I made this short program, which doesn't work (loops endlessly). I'm pretty sure I shouldn't be putting a while-loop inside a for-loop, but I'm not sure how exactly to fix this.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char word[80];
cin >> word;
int length = strlen(word);
int counter = 1;
for (int i = 0; i < length; i++) {{
while (word[i] == word[i + 1]) {
counter++;
}
cout << counter << word[i];
}
return 0;
}
Similarly, if I have to print out a long word having a short one, I made a program which also doesn't work (output is a bunch of hieroglyphs):
#include <iostream>
#include <string.h>
using namespace std;
int number = 0;
bool Number(char c) {
switch(c) {
case '1':
number = 1;
return true;
break;
case '2':
number = 2;
return true;
break;
case '3':
number = 3;
return true;
break;
case '4':
number = 4;
return true;
break;
case '5':
number = 5;
return true;
break;
case '6':
number = 6;
return true;
break;
case '7':
number = 7;
return true;
break;
case '8':
number = 8;
return true;
break;
case '9':
number = 9;
return true;
break;
case '0':
number = 0;
return true;
break;
default:
return false;
}
}
int main()
{
char word[80];
cin >> word;
int length = strlen(word);
int counter = 1;
for (int i = 0; i < length; i++) {
if (Number(word[i])) {
for (int j = 0; j < number; i++) {
cout << word[i];
}
} else {
continue;
}
}
return 0;
}
Not sure if this is what you want but I assumed that there can be 2 or more consecutive digits and only one character after the digits.
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int
main ()
{
char word[80];
char out[128];
char temp[10];
cin >> word;
int len = strlen (word);
int index = 0;
int numindex = 0;
int count;
while (index < len) {
while (word[index] >= '0' && word[index] <= '9') {
temp[numindex++] = word[index++];
}
temp[numindex] = 0;
count = atoi (temp);
numindex = 0;
char ch = word[index++];
for (int i = 0; i < count; i++) {
out[i] = ch;
}
out[count] = 0;
cout << out;
}
return 0;
}
I'm a C++ noob coding the snake game. The whole program draws the board, fruit and snake head perfectly, however I cannot seem to make the snake head coordinates change using a keyboard hit function. When going through the input function which gets keyboard strokes for snake movement, it appears that the if(kbhit) evaluates to false when running program,what is my error and how can I get the snake head to move?
Thanks.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
using namespace std;
int X, Y, fruitX, fruitY;
enum eDir { STOP = 1, UP = 2, DOWN = 3, RIGHT = 4, LEFT = 5 }; //declare variables
eDir direction;
const int height = 20;
const int width = 20;
int board[height][width];
bool gameOver;
void setup()
{
gameOver = false;
srand(time(NULL)); // initilize game set up
fruitX = (rand() % width);
fruitY = (rand() % height);
X = height / 2;
Y = width / 2;
direction = STOP;
int board[height][width];
};
void draw()
{
system("cls");
for (int i = 0; i < width + 2; i++)
cout << '#';
cout << endl;
for (int i = 0; i < height; i++) //loop through matrix to draw board
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << '#';
if (i == Y && j == X) // draw snake head
cout << 'T';
else if (i == fruitY && j == fruitX) // draw fruit
cout << 'F';
else
cout << ' ';
if (j == width - 1)
cout << '#';
}
cout << endl;
}
for (int i = 0; i < width + 2; i++)
cout << '#';
cout << endl;
};
void input()
{
if (_kbhit())
{
switch (_getch())
{
case 'w':
direction = UP;
break;
case 's':
direction = DOWN;
break;
case 'a':
direction = LEFT;
break;
case 'd':
direction = RIGHT;
break;
default:
break;
}
}
};
void logic()
{
switch (direction)
{
case UP:
Y--;
break;
case DOWN:
Y++;
break;
case LEFT:
X--;
break;
case RIGHT:
X++;
break;
default:
break;
}
};
int main()
{
setup();
while (!gameOver)
{
draw();
input();
logic();
return 0;
}
};
For one thing, you do return 0; inside your game loop which kills the program after only one iteration which I assume you did not intend.
I made snake using a similar method and mine looks like this:
bool play = true;
while(play) {
while (_kbhit) {
switch (_getch) {
case 'w':
//code
break;
case 's':
//code
break;
case 'a':
//code
break;
case 'd':
//code
break;
}
}
}
That method worked for me and I think the only difference is that I used a while loop instead of an if.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want my function to read one char[] and split it into 2 arrays. Both arrays would be one big number, first array is for that number int value and second one is for double values. I must use 2 arrays because this number can be binary, decimal, octal or hex.
void Read(int place[], int &size, int &type, int placedot[], int &sizedot, int &typedot)
{
char reader[limit];
cin >> reader;
size = 1;
while (reader[size] == '.' || size != strlen(reader))
{
size++;
cout << "LOL";
}
cin >> type;
cout << size;
typedot = type;
for (int i = 0; i<size; i++)
{
switch (reader[i])
{
case '0': place[i] = 0; break;
case '1': place[i] = 1; break;
case '2': place[i] = 2; break;
case '3': place[i] = 3; break;
case '4': place[i] = 4; break;
case '5': place[i] = 5; break;
case '6': place[i] = 6; break;
case '7': place[i] = 7; break;
case '8': place[i] = 8; break;
case '9': place[i] = 9; break;
case 'A': place[i] = 10; break;
case 'B': place[i] = 11; break;
case 'C': place[i] = 12; break;
case 'D': place[i] = 13; break;
case 'E': place[i] = 14; break;
case 'F': place[i] = 15; break;
}
}
}
#include <iostream>
#include <string>
int main()
{
std::string str = "3423432432.32445654576654578978905";
std::string strInt;
std::string strDec;
int i = 0;
while('.' != str[i])
strInt += str[i++];
for(++i; i < str.length(); i++)
strDec += str[i];
std::cout << strInt << std::endl << strDec << std::endl;
// now you have the integer part and the decimal part as strings so convert each of them to int
std::cout << std::endl;
return 0;
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
For the 3 switch statements, I am to replace each switch statement with two or more function calls. This is to replace an "incomplete" in my beginning C++ class last semester so that I can get a loan and I've got no idea where to start.
I tried quite literally taking the switch statements and putting them into their own functions, of the switch statements themselves only, and, of course, that created numerous syntax errors (e.g. counter, random_number). I've no idea how to do it, how to return proper values to main() and get the program to communicate with other parts of the program (e.g. variables defined/initialized in main). As one can see, I'm just pretty lost here and would like some guidance toward figuring this out. I'm not asking anyone to do this for me, just some guidance (my knowledge on C++ is limited and there are time constraints).
// random.cpp : Defines entry point for the console application.
//
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//random number generator prototypes
void randomize(void);
void randomize(int seed);
int random(void);
int random(int upper_bound);
int random(int upper_bound, int lower_bound);
int main()
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
randomize();
int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;
for(int counter = 1; counter <= n_random_numbers; counter++)
{
int random_number = random(upper_bound, lower_bound);
int digit_1 = random_number % 10; random_number = random_number / 10;
switch(digit_1)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
int digit_2 = random_number % 10; random_number = random_number / 10;
switch(digit_2)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
int digit_3 = random_number % 10; random_number = random_number / 10;
switch(digit_3)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
}
cout << "0 occurs " << counter_0 << " times" << endl;
cout << "1 occurs " << counter_1 << " times" << endl;
cout << "2 occurs " << counter_2 << " times" << endl;
cout << "3 occurs " << counter_3 << " times" << endl;
cout << "4 occurs " << counter_4 << " times" << endl;
cout << "5 occurs " << counter_5 << " times" << endl;
cout << "6 occurs " << counter_6 << " times" << endl;
cout << "7 occurs " << counter_7 << " times" << endl;
cout << "8 occurs " << counter_8 << " times" << endl;
cout << "9 occurs " << counter_9 << " times" << endl;
system("pause");
return 0;
}
//random number generators
void randomize(void)
{
srand(unsigned(time(NULL)));
}
void randomize(int seed)
{
srand(unsigned(seed));
}
int random(void)
{
return rand();
}
int random(int upper_bound)
{
return rand() % (upper_bound + 1);
}
int random(int upper_bound, int lower_bound)
{
if(upper_bound < lower_bound)
{
int t = upper_bound;
upper_bound = lower_bound;
lower_bound = t;
}
int range = upper_bound - lower_bound + 1;
int number = rand() % range + lower_bound;
return number;
}
I added these functions, to see if it would work (Figure 1.1)
(I added counter, upper_bound, lower_bound, n_random_numbers because I couldn't figure out how to have the function read those variables from main(). I tried making them into a function, calling them in main, and calling them in my created functions, but that definitely didn't work. These added functions replace the switches in the original with function calls (see: Figure 1.2). It compiles, but the output returns that "0 occurs 0 times, 1 occurs 0 times, etc."
FIGURE 1.1
int switch1 (int switch_1)
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
int counter = 0;
randomize();
int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;
int random_number = random(upper_bound, lower_bound);
int digit_1 = random_number % 10; random_number = random_number / 10;
switch(digit_1)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
}
int switch2 (int switch_2)
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
int counter = 0;
randomize();
int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;
int random_number = random(upper_bound, lower_bound);
int digit_2 = random_number % 10; random_number = random_number / 10;
switch(digit_2)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
}
int switch3 (int switch_3)
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
int counter = 0;
randomize();
int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;
int random_number = random(upper_bound, lower_bound);
int digit_3 = random_number % 10; random_number = random_number / 10;
switch(digit_3)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
}
FIGURE 1.2
for(int counter = 1; counter <= n_random_numbers; counter++)
{
int random_number = random(upper_bound, lower_bound);
int digit_1 = random_number % 10; random_number = random_number / 10;
int digit_2 = random_number % 10; random_number = random_number / 10;
int digit_3 = random_number % 10; random_number = random_number / 10;
switch1 (digit_1);
switch2 (digit_2);
switch3 (digit_3);
}
No switch needed:
int counters[10] = {};
for(int counter = 1; counter <= n_random_numbers; counter++)
{
int random_number = random(upper_bound, lower_bound);
int digit_1 = random_number % 10; random_number = random_number / 10;
++counters[digit_1];
int digit_2 = random_number % 10; random_number = random_number / 10;
++counters[digit_2];
int digit_3 = random_number % 10; random_number = random_number / 10;
++counters[digit_3];
}