So, I've been writing a program that adds two Roman numbers and outputs the sum in Roman numbers as well. The issue I ran was and is, that the thing that checks the result is only reading the first output letter, not the entire output.
Which made me think about how to fill the string with chars (or strings with a length of one). I do something like:
#include <iostream>
int main() {
std::string str{" "};
for (int i = 0; i < 5; ++i) {
str[i] = 'A';
}
std::cout << str;
}
After running this, you get output "A", when I want it to be "AAAAA". I just really don't know how to make a list of chars dynamic one, if to say technical.
For context, here is the code for the Roman numerals. I tried doing this in Python, but it didn't work out well too, and since I initially started in C++, I want to know how to do such an operation there.
#include <iostream>
using namespace std;
// simple shit
// a func str gets a sum, then with while loops 'fills' the " " in the string s with some letters
string str(int sum) {
string s = " ";
int i = 0;
while (sum >= 1000) {
s[i] = 'M';
sum -= 1000;
++i;
}
while (sum >= 500) {
s[i] = 'D';
sum -= 500;
++i;
}
while (sum >= 100) {
s[i] = 'C';
sum -= 100;
++i;
}
while (sum >= 50) {
s[i] = 'L';
sum -= 50;
++i;
}
if (sum == 19) {
s[i] = 'X';
++i;
s[i] = 'I';
++i;
s[i] = 'X';
++i;
sum -= 19;
}
while (sum >= 10) {
s[i] = 'X';
++i;
}
if (sum == 19) {
s[i] = 'X';
++i;
s[i] = 'I';
++i;
s[i] = 'X';
++i;
sum -= 19;
}
if (sum == 9) {
s[i] = 'I';
++i;
s[i] = 'X';
++i;
sum -= 9;
}
while (sum >= 5) {
s[i] = 'V';
++i;
}
if (sum == 4) {
s[i] = 'I';
++i;
s[i] = 'V';
++i;
sum -= 4;
}
while (sum >= 1) {
s[i] = 'I';
++i;
}
for (int i = 0; i < s.length(); ++i) {
if (s[i] == ' ') {
s[i] = '\0';
}
}
//..and returns the string, full of " ", when I don't need those
return s;
}
//uh, that's the func to check if IX == 19, not 21. don't care about this one
int a(string prev) {
if (prev == "M") {
return 1000;
}
if (prev == "D") {
return 500;
}
if (prev == "C") {
return 100;
}
if (prev == "L") {
return 50;
}
if (prev == "X") {
return 10;
}
if (prev == "V") {
return 5;
}
if (prev == "I") {
return 1;
}
else {
return 0;
}
}
int main() {
//all the strings and other cool stuff
string s, letter, prev;
cin >> s;
int sum;
int prev_check;
int l = s.length();
// a loop to find a letter in a string we just input, and 'parse' it to the normal-digits (arabic) form
for (int i = 0; i < l; i++) {
letter = s[i];
if (letter == "M") {
sum += 1000;
//checking if nothing less than current Rome digit is behind it
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 1000) {
sum -= prev_check * 2;
//you may be wondering, why times two?
//because I've already added the letter behind to my sum,
//so I have to substract it from sum twice
}
}
}
// fun fact - there cant exist numbers such as IM (999) in Rome letter, only C, X and V are "substractable"
//yet, I've still added this kinda "substraction" to all letters, just because I'm lazy to input specific ones for debugging my "substraction" func
if (letter == "D") {
sum += 500;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 500) {
sum -= prev_check * 2;
}
}
}
if (letter == "C") {
sum += 100;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 100) {
sum -= prev_check * 2;
}
}
}
if (letter == "L") {
sum += 50;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 50) {
sum -= prev_check * 2;
}
}
}
if (letter == "X") {
sum += 10;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 10) {
sum -= prev_check * 2;
}
}
}
if (letter == "V") {
sum += 5;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 5) {
sum -= prev_check * 2;
}
}
}
if (letter == "I") {
sum += 1;
}
}
//and... out
cout << str(sum);
}
It's quite some code, I know, I added a few comments explaining what am I doing here and there.
The below code might help to output 5 'A'
#include <iostream>
#include <vector>
int main() {
std::vector<char> val;
for (int i = 0; i < 5; ++i) {
val.push_back('A');
}
for (int a = 0; a < val.size(); ++a)
{
std::cout << val[a];
}
}