Ok, so this program has been shortened to make it easier to read. There would obviously be more case statements to go along with the amount of variables there are. My question is why does my program get an error every time it runs. It is fine when it compiles but not when I run it.
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string thearray[22960];
char bigfor[10];
int arrayvar = 0;
int finishedarray;
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
int j;
// Constants
int variable1 = 0;
int variable2 = 1;
int variable3 = 2;
int variable4 = 3;
int variable5 = 4;
int variable6 = 5;
int variable7 = 6;
int variable8 = 7;
int variable9 = 8;
int variable10 = 9;
for (a = 0; a < 36; a++)
{
switch (a)
{
case 0:
bigfor[variable1] = '0';
break;
case 1:
bigfor[variable1] = '1';
break;
case 2:
bigfor[variable1] = '2';
break;
case 3:
bigfor[variable1] = '3';
break;
case 4:
bigfor[variable1] = '4';
break;
case 5:
bigfor[variable1] = '5';
break;
case 6:
bigfor[variable1] = '6';
break;
case 7:
bigfor[variable1] = '7';
break;
case 8:
bigfor[variable1] = '8';
break;
case 9:
bigfor[variable1] = '9';
break;
}
thearray[arrayvar] = bigfor[variable1] + bigfor[variable2] + bigfor[variable4] +
bigfor[variable5] + bigfor[variable6] + bigfor[variable7] +
bigfor[variable8] + bigfor[variable9] + bigfor[variable10];
arrayvar = arrayvar + 1;
}
finishedarray = arrayvar + 1;
ofstream myfile;
myfile.open("codes.txt");
for (arrayvar = 0; a < finishedarray; a++)
{
myfile << thearray[arrayvar] << endl;
}
myfile.close();
return 0;
}
One problem is that you are reading uninitialized values here:
thearray[arrayvar] = bigfor[variable1]+ bigfor[variable2] + bigfor[variable4] +
bigfor[variable5] + bigfor[variable6] + bigfor[variable7] + bigfor[variable8] +
bigfor[variable9] + bigfor[variable10];
This is undefined behaviour. At this point in the program, only bigfor[variable1] is initialized.
Whatever you are trying to do, I have a feeling there is a simpler way to do it.
The problem was the addition you were doing. The addition will only add the integer-encoded values of the ASCII characters, and result in an integer. This will cause an error because you can't assign an integer to a std::string.
In addition to #juanchopanza's answer, your code can be improved substantially. For example, instead of listing out the indices manually, you can do this instead within your for loop:
std::string bigfor;
for (int a = 0; a < 36; ++a)
{
bigfor += (a + 48);
}
std::ofstream myfile("codes.txt");
for (int i = 0; i < bigfor.size(); ++i)
{
myfile << bigfor[i] << std::endl;
}
Related
below is c++ program for decimal to hexadecimal conversion!!
// decimal to hexadecimal conversion
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
string d2h(int n) {
string s, t;
int i = 0;
while (n >= 1) {
t[i] = n % 16;
switch (t[i]) {
case 10:
t[i] = 'A';
break;
case 11:
t[i] = 'B';
break;
case 12:
t[i] = 'C';
break;
case 13:
t[i] = 'D';
break;
case 14:
t[i] = 'E';
break;
case 15:
t[i] = 'F';
break;
default:
break;
}
s[i] = t[i];
n /= 16;
i++;
}
return s;
}
int main() {
int n;
cin >> n;
cout << d2h(n);
return 0;
}
I am getting nothing as output!!
PS C:\Users\anmol\Desktop\c++projectwork> g++ .\dec2hexadec.cpp
PS C:\Users\anmol\Desktop\c++projectwork> ./a
479
PS C:\Users\anmol\Desktop\c++projectwork>
What should i change??
You are not allocating space in s or t for memory. Use s.push_back() instead of s[i].
You'll probably need to reverse the string as well.
t[i] = n % 16; This does not assign the character '0', but rather the number 0. Why don't you just create 16 cases in your switch statement? Or use the ASCII quirk '0' + (n % 16).
// decimal to hexadecimal conversion
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
string d2h(int n) {
string s, t;
string Sign;
int i = 0;
// n is equal to 0? <0 or >0?
if (n == 0){
return "0";
}
if (n < 0){
Sign = "-";
n *= -1;
}
while (n ) {
t.push_back(n % 16) ;
switch (t[i]) {
case 0:
t[i] = '0';
break;
case 1:
t[i] = '1';
break;
case 2:
t[i] = '2';
break;
case 3:
t[i] = '3';
break;
case 4:
t[i] = '4';
break;
case 5:
t[i] = '5';
break;
case 6:
t[i] = '6';
break;
case 7:
t[i] = '7';
break;
case 8:
t[i] = '8';
break;
case 9:
t[i] = '9';
break;
case 10:
t[i] = 'A';
break;
case 11:
t[i] = 'B';
break;
case 12:
t[i] = 'C';
break;
case 13:
t[i] = 'D';
break;
case 14:
t[i] = 'E';
break;
case 15:
t[i] = 'F';
break;
default:
break;
}
s.push_back(t[i]);
n /= 16;
i++;
}
reverse(s.begin(), s.end());
return Sign+s;
}
int main() {
int n;
cin >> n;
cout << d2h(n);
return 0;
}
Well, i made RGB To Hex Conversion. My programm in online compiler works pretty well, but on codewars it has a problem "control may reach end of non-void function". What should i do? Should I remove for loop?
using namespace std;
#include <iostream>
#include <string>
int checkForNumber(int input) {
if (input > 255) return 255;
if (input < 0) return 0;
else return input;
}
string check(int input) {
if (input < 10) return to_string(input);
switch (input)
{
case 10:
return "A";
break;
case 11:
return "B";
break;
case 12:
return "C";
break;
case 13:
return "D";
break;
case 14:
return "E";
break;
case 15:
return "F";
break;
}
}
**string YES(int input) {
string m;
char temp;
for (int i = 0; i < 2; i++) {
m += check(input % 16);
input /= 16;**
//problem is somewhere here
}
temp = m[0];
m[0] = m[1];
m[1] = temp;
return m;
}
int main()
{
string output;
ios_base::sync_with_stdio(false);
int r = 148 , g = 0, b = 211;
r = checkForNumber(r);
g = checkForNumber(g);
b = checkForNumber(b);
output += YES(r) + YES(g) + YES(b);
cout << output;
}
The problem is in your check(int input) function -- consider what happens if the value of the input argument is not in the range [10, 15]. I suggest putting in a default: case that returns something appropriate at the bottom of your switch block.
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 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;
}
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 8 years ago.
Improve this question
i have a program and i should convert int to char into it
but i cant use itoa() because site's judge don't support it
so i wrote this:
xt[i]= rmn+'0';
but i get this error :
Runtime error: Illegal file open (/dev/tty)
How should i convert this?
My code is this : (for palsquare question of USACO)
/*
ID: sa.13781
PROG: palsquare
LANG: C++
*/
#include <iostream>
#include <fstream>
using namespace std;
ofstream fout("palsquare.out");
int tool(char xt[])//CORRECT
{
int p = 0;
while (xt[p] != 0)
p++;
return p;
}
void prt(char xt[])//CORRECT
{
int p = 0;
while (xt[p] != 0)
{
fout << xt[p];
p++;
}
}
void mabna(int a, char xt[], int mab)
{
int ex = 1, tavan = 0, rmn, n;
for (; ex <= a; ex *= mab)
tavan++;
for (int i = tavan - 1; a != 0; i--, a /= mab)
{
rmn = a % mab;
switch (rmn)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
xt[i] = rmn + '0';
break;
case 10:
xt[i] = 'A';
break;
case 11:
xt[i] = 'B';
break;
case 12:
xt[i] = 'C';
break;
case 13:
xt[i] = 'D';
break;
case 14:
xt[i] = 'E';
break;
case 15:
xt[i] = 'F';
break;
case 16:
xt[i] = 'G';
break;
case 17:
xt[i] = 'H';
break;
case 18:
xt[i] = 'I';
break;
case 19:
xt[i] = 'J';
break;
}
}
}
bool mirror(char *xt)//CORRECT
{
int p = 0;
int n = tool(xt);
for (int i = 0; i < (n / 2); i++)
if (xt[i] == xt[n - i - 1])
p++;
if (p == (n / 2))
return true;
return false;
}
void calc(int mab) //CORRECT
{
for (int i = 1; i <= 300; i++)
{
char p[10] = {0}, p2[10] = {0};
mabna(i * i, p2, mab);
if ( mirror(p2) == true )
{
mabna(i, p, mab);
prt(p);
fout << " ";
prt(p2);
fout << "\n";
}
}
}
int main()
{
ifstream fin("palsquare.in");
int mab;
fin >> mab;
calc(mab);
return 0;
}
Can you use sprintf()?
char s[16];
int x = 15;
sprintf(s, "%d", x);
If your int value is single digit, you can just cast it with (char)
If your int value is more than one digit, you can never expect a single char to hold it. char can only hold single character.
int num = 7;
char ch = (num + 48); //same effect as + '0'
cout << ch << endl;
OUTPUT: 7
If your int value is more than single digit. You need char* or char[] to hold the converted data. For example:
int num = 123987;
int len = 6;
int idx = len-1;;
char ch[20];
while(num > 0)
{
ch[idx] = (num%10) + 48;
num /= 10;
idx --;
}
for(int x=0; x<len; x++)
cout << "OUTPUT in char: " << ch[x];
OUTPUT in char: 123987
This is not a perfectly good way to handle this kind of situation, but it may gives you what you asked for.