Trouble displaying Roman Numerals using ones, tens, hundredths, and thousandths place - c++

My code needs to take into consideration the years 2000 to 2100 and display them as Roman Numerals.
For instance, 2000: XXXIV.
I started out by initiating a digit check: const int DIGIT_CHECK = 10 which I believe splits and one or two digit number.
Then for the ones place I initialized a unit_digit and % it to DIGIT_CHECK int unit_digit = n % DIGIT_CHECK;
I believe you have to do the same thing for the tenths, hundredths, and thousandths place: int tenth_digit = n / DIGIT_CHECK; int hundredth_place = n / 100; int thousandth_digit = n / 1000;
Then I listed out the roman numerals in strings for the numbers one, four, five, nine, ten, fifty, hundred, thousand: const string ONE = "I"; and so on...
I then started a switch case (as my professor calls it) switch (unit_digit) that takes in every possible roman numeral arrangement in the ones digit " I, II, III, IV, V, VI, VII, VIII, IX, and X": case 1: roman = ONE; case 2: roman = ONE + ONE;...
I am having a problem with the tenth digit when approaching the year 2076. It's not that the code doesn't work, it's just that as it approaches 2076 is prints off: 2075 --> CIX 2076 --> C when 2076 should be CX. So something is missing in the tenths place switch case: case 1: roman = TEN + roman; break; case 2: roman = TEN + TEN + roman; break; case 3: roman = TEN + TEN + TEN + roman; break; case 4: roman = TEN + FIFTY + roman; break; case 5: roman = FIFTY + roman; break; case 6: roman = FIFTY + TEN + roman; break; case 7: roman = FIFTY + TEN + TEN + roman; break; case 8: roman = FIFTY + TEN + TEN + TEN + roman; break; case 9: roman = TEN + HUNDRED + roman; break; default: break;
I have run a debugger and have looked at the forums so it is ok if I can't get any assistance. Just thought I would tweak my question to fit the comments better. Sorry.

Related

discount value cannot calculate after case commad

I have some problem with my homework. So this is how it looks like
#include<stdio.h>
int main()
{
char code;
int price,discount;
float total;
printf("Please input price: ");
scanf("%d",&price);
printf("Please input discount code: ");
scanf(" %c",&code);
switch(code)
{
case 'a': printf("Your discount code is 25 percent\n");
discount = 25;
break;
case 'b': printf("Your discount code is 15 percent\n");
discount = 15;
break;
case 'c': printf("Your discount code is 5 percent\n");
discount = 5;
break;
default: printf("Wrong code,Your discount is 0 percent\n");
discount = 0;
break;
}
total = (price*((100-discount)/100));
printf("Your price is = %.2f\n",total);
}
I have 2 questions to ask
My task is I have to input both of uppercase and lowercase letter for discount code (there are only three codes: a, b, c) so how can I put both of them in case command? (in this I only do the lowercase letter)
I have run this. But it seems like the discount value is 0 when I try to used it for calculate in the end. When I print the discount only, it works normally. How can I fix that?
Sorry for my poor English and Thank you for your help!
There would be different possibilities.
scanf(" %c",&code);
switch(code)
{
case 'a':
case 'A':
printf("Your discount code is 25 percent\n");
discount = 25;
break;
or you change the input to lower case before or in the switch!
switch( code | 0x60 ) // make it to lower case
with this you don't have to change the following code.

long equation addition/subtraction calculator that pulls operator and numbers from a text file

I have this assignment:
We want to make a simple calculator that can add and subtract integers, and will accept arbitrarily long mathematical formulas composed of symbols + and - and non-negative integer numbers.
Imagine you have a file formula.txt with the summation formula like:
100 + 50 - 25 + 0 + 123 - 1
So far I've made a program that will read the file, and locate only the first operand and two numbers ( So basically it only does the first condition, I.E '100 + 50 - 25' gives me 150...) I just need help understanding how my program can GO BACK after doing the first round...I'm sure it's something wrong with my switch statements.
Thank you so much!
char op;
int left_num, right_num, sum;
sum = 0;
while(cin >> left_num >> op >> right_num) {
switch(op) {
case '+':
sum_new = left_num + right_num;
sum = sum_new + sum
break;
case '-':
sum = sum - right_num
break;
}
}
I recommend a special case for the first value. All the remaining terms are of the form operator value:
int sum;
std::cin >> sum; // Read the first value as the sum.
char opr; // Operator character
int value; // New value or term.
while (cin >> opr >> value)
{
switch (opr)
{
case '+':
sum += value;
break;
case '-':
sum -= value;
break;
default:
std::cerr << "Invalid operator: " << opr << "\n";
break;
}
}
The sum will always be the "left" side of the operator. The new value will be the "right" side of the operator.
Well you could do something along this line:
1. Read the first operand (let's say num)
2. While there are operator and operands to read
2.1. Your switch updates the first operand (num)
3. num is the result

How to get specific output for a certain run time of a for loop?

I am trying to design a program that outputs the modulus of each fibonacci number given a certain number of terms to run. I also want this program to output when the number is even, divisible by 5, is a zero, or all three. so essentially it needs a specific output for every third, fifth and fifteenth time the loop is run hence why I tried using a bunch of if statements. If you could please guide me in the right direction or tell me what kind of code/format to use i think i'll be able to figure out. Thanks.
sample output: for a 17 term input
1: 1
2: 1
3: 2 - even
4: 3
5: 5 - divisible by 5
6: 8 - even
7: 3
8: 1
9: 4 - even
10: 5 - divisible by 5
11: 9
12: 4 - even
13: 3
14: 7
15: 0 - even -divisible by 5 - ends in zero
16: 7
17: 7
#include <iostream>
using namespace std;
int main ()
{
int terms, n, next1, next2, first = 0, second = 1;
cout << "Welcome to the Fibonacci sequence checker!" << endl;
cout << "How many terms do you want to check?" << endl;
cin >> terms;
for ( int n = 1; n < terms; n++)
{
if (n <= 1)
next2 = n;
else
{
next1 = first + second;
next2 = (first + second) % 10;
first = second;
second = next1;
}
if(next2 == 2 || next2 == 4 || next2 == 6 || next2 == 8)
{
cout << next2 << " - even";
}
if(next2 == 5)
{
cout << next2 << " - divisible by 5";
}
if(next2 == 0)
{
cout << " - even - divisible by 5 - ends in a zero";
}
}
return 0;
}
sample output:
1: 1
2: 1
3: 2 - even
4: 3
5: 5 - divisible by 5
6: 8 - even
7: 3
8: 1
9: 4 - even
10: 5 - divisible by 5
11: 9
12: 4 - even
13: 3
14: 7
15: 0 - even -divisible by 5 - ends in zero
16: 7
17: 7
First of all, to check if the number is even just check the modulus by 2, no need to check if the last digit is 2 or 4 or 6 or 8. I would substitute all your checks with next1%2==0, next1%5==0 and next1%10==0, also you need to change every cout<<next2 to cout<<next1 because next2 is modulus of 10 of next1. Also as a good practice I suggest you to read Straustrup C++ book to get the basics of C++.

My program doesn't quite work right

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
string numString(int k)
{
string str;
switch (k) {
case 0 : str ="ZERO"; break;
case 1 : str ="ONE"; break;
case 2 : str ="TWO"; break;
case 3 : str ="THREE"; break;
case 4 : str ="FOUR"; break;
case 5 : str ="FIVE"; break;
case 6 : str ="SIX"; break;
case 7 : str ="SEVEN"; break;
case 8 : str ="EIGHT"; break;
case 9 : str ="NINE"; break;
}
return str;
}
int main(int argc, char** argv) {
int value;
int digit;
cout << "Enter a number: ";
cin >> value;
while (value > 0 )
{
digit = value % 10;
value = value / 10;
cout << numString(value) << endl;
}
return 0;
}
My program is supposed to prompt the user for an integer value read that value, and output the word equivalent of each digit
For example if "9502" was entered it would output "NINE FIVE ZERO TWO"
Yet mine only outputs "NINE ZERO" and then stops. I can't figure out what I'm doing wrong, any help at all is greatly appreciated.
Yet mine only outputs "NINE ZERO" and then stops.
That's literally what you told it to do!
while (value > 0 )
You'll have to find some other way to signal a "termination" condition if zero is supposed to be valid.
Traditionally we let the system's own end-of-file signalling take care of that for us, so that we don't have to "reserve" some otherwise-valid input and use it as a signal flag.
while (cin >> value) {
digit = value % 10;
value = value / 10;
cout << numString(value) << endl;
}
That fixes your loop, although unfortunately your maths are still wrong and your output is not what you intend. In fact, your digit variable is not used at all. I am not going to spoonfeed an algorithm here: you'll have to work out on paper how to achieve the business logic. :)
This is an analysis of your program using the input 9502.
Iteration 1, in main:
(new) value = 9502 / 10;
(new) digit = 9502 % 10;
value == 950;
digit == 2;
call numString(950);
// Note: there is no case for 950 in numString so an empty string is returned.
Iteration 2, in main:
(new) value = 950 / 10;
(new) digit = 950 % 2;
value == 95;
digit == 0;
call numString(95);
// Note: there is no case for 95 in numString so an empty string is returned.
The above iterations and variable values can be obtained with pen & paper (or a note pad). You can also do the same with something called a debugger.
I hope this helps.

Using escape sequence at runtime with C++

I am quiet new with C++ and I need to read an input from a MSVC++ text-field and write it to a file. I need to write \n as a new line to the file and not as \n.
After some researching I found that escape characters only work at compile-time. Is it possible for me to use it on run-time. I am only using C++ for this task.
I might write this a bit differently if I were doing it in C++ today (I wrote this in C around 20 years ago), but it might at least provide a little inspiration:
/*
** Public Domain by Jerry Coffin.
**
** Interprets a string in a manner similar to that the compiler
** does string literals in a program. All escape sequences are
** longer than their translated equivalant, so the string is
** translated in place and either remains the same length or
** becomes shorter.
*/
#include <string.h>
#include <stdio.h>
#include "snip_str.h"
char *translate(char *string)
{
char *here=string;
size_t len=strlen(string);
int num;
int numlen;
while (NULL!=(here=strchr(here,'\\')))
{
numlen=1;
switch (here[1])
{
case '\\':
break;
case 'r':
*here = '\r';
break;
case 'n':
*here = '\n';
break;
case 't':
*here = '\t';
break;
case 'v':
*here = '\v';
break;
case 'a':
*here = '\a';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
numlen = sscanf(here,"%o",&num);
*here = (char)num;
break;
case 'x':
numlen = sscanf(here,"%x",&num);
*here = (char) num;
break;
}
num = here - string + numlen;
here++;
memmove(here,here+numlen,len-num );
}
return string;
}