Using switch case - c++

This is what I have so far. It won't work. I'm trying to write it to get user input of an amount and the county they live in and the program outputs the total with taxes
#include<iostream>
using namespace std;
int main()
{
char amt = 0;
double county ;
char x = 0;
double total = 0;
total = amt + x;
x = county;
printf("\nplease enter amount\n");
scanf_s("%d",&amt);
printf("\nplease enter county\n");
scanf_s("%c",&x);
scanf_s("%c",&total );
printf("total:", amt * x);
switch(x)
{
case 'o':
printf("orange:",county = 0.06);
break;
case 'l':
printf("lake:",county = 0.07);
break;
case 's':
printf("seminole:",county = 0.08);
break;
}
system("pause");
}

When you use printf(), also specify which variables you are sending, so if you want to print a value use something like: printf("Value: %f\n", 0.07);.
You can find the format specifiers here.

You should put the switch clause before the line where you print the total. Also, from what I'm seeing you shouldn't multiply the amt * x but something like subtotal * (1 + county)

If scanf_s() is anything like scanf(), you should consider using the right conversions for the arguments ("%d" means 'int' and so on).

Related

Why does the console asks for more inputs? C++

I am trying to make a program where I calculate fees based on the ride type and time the user rented stuff. I came up with the method of how to do it, but when I try to read input the console keeps asking for more input.
double computeFee(){
double fee, hour_rate, minute_rate;
char ride_type, colon;
int hour_started, minute_started, hour_ended, minute_ended, total_minutes;
cin >> ride_type >> hour_started >> colon >> minute_started >> colon >> hour_ended >> colon >> minute_ended;
total_minutes = ((hour_ended * 60) + minute_ended) - ((hour_started * 60) + minute_started);
switch(ride_type){
case 'J':
hour_rate = 500;
minute_rate = 10;
break;
case 'K':
hour_rate = 200;
minute_rate = 5;
break;
case 'B':
hour_rate = 100;
minute_rate = 2;
break;
case '0':
return 1;
break;
}
if (total_minutes < 60){
fee = total_minutes * minute_rate;
}else{
fee = ((total_minutes/60) * hour_rate) + ((total_minutes%60) * minute_rate);
}
return fee;
}
int main(){
double total, fee;
computeFee();
fee = computeFee();
cout << fee;
return 0;
}
Each time you call computeFee(), the function code is executed. In your code, you call it twice, so it will ask you for the input two times. And given that the first time you call it you don't assign it, the first input will be lost.
To make it work the way you want, your main code should be:
int main(){
double fee;
fee = computeFee();
cout << fee;
return 0;
}

My program isnt working after the scanf statement

My code isn't working after the second scanf statement. I'm still new to C++ so im not sure what I am doing wrong. I've looked on this website as well as others and so far havent found anything. I've also tried the fflush statement but that didnt fix it either, any other suggestions would be great thanks.
include <stdio.h>
int main(void)
{
//Intro message
printf("This program will take the order of a customer and then subtract their order from the inventory list. After calculations a new inventory list will appear\n");
// declare variables
int dough, sauce, cheese, pepperoni, sausage, greenPeppers, blackOlives, mushrooms;
int num1;
char pizza;
// create inventory
dough = 50;
sauce = 50;
cheese = 50;
pepperoni = 50;
sausage = 50;
greenPeppers = 50;
blackOlives = 50;
mushrooms = 50;
//display the inventory
printf("\n The currrent inventory is: \n Dough:%d", dough);
printf("\n Sause:%d", sauce);
printf("\n Cheese:%d", cheese);
printf("\n Pepperoni:%d", pepperoni);
printf("\n Sausage:%d", sausage);
printf("\n Green Peppers:%d", greenPeppers);
printf("\n Black Olives%d", blackOlives);
printf("\n Mushrooms:%d", mushrooms);
//get customer input
printf("\nWhat type of pizza would you like? Please type a lowercase letter and only chose one. Type C for Cheese, P for Pepperoni, S for Sausage, and V for Veggie.\n");
scanf_s(" %c", &pizza);
printf("\nHow many pizzas would you like to order? Maximum of 10.\n");
scanf_s(" %d", &num1);
//This is where it stops
printf("It cuts out here, why can I not see anything after this line?");
// calculate what inventory will be taken out
cheese = cheese - num1;
dough = dough - num1;
sauce = sauce - num1;
switch (pizza)
{
case 'c':
cheese = 50 - num1;
break;
case 'p':
pepperoni = pepperoni - num1;
break;
case 's':
sausage = sausage - num1;
break;
case 'v':
blackOlives = blackOlives - num1;
greenPeppers = greenPeppers - num1;
mushrooms = mushrooms - num1;
break;
}
// display final inventory
printf("The new inventory is: \n Dough:%d", dough);
printf("\n Sause:%d", sauce);
printf("\n Cheese:%d", cheese);
printf("\n Pepperoni:%d", pepperoni);
printf("\n Sausage:%d", sausage);
printf("\n Green Peppers:%d", greenPeppers);
printf("\n Black Olives%d", blackOlives);
printf("\n Mushrooms:%d", mushrooms);
return 0;
}
The problem is that you are using scanf (or its cousin scanf_s). The scanf problem is not suitable for interactive input, and leads to confusing results like what you're seeing.
Instead, use the fgets function to read data into a character array. If you need to convert that character array into a number, use atoi.

C++ read number string and capture specific position character as int

I need my program to read a string of numbers input by the user and then assign each number to an int variable:
94715 is input by the user as string
then
a=9
b=4
c=7
d=1
e=5
so I can
if (a < b), c*d+e, a-e, etc
I've searched some commands (getline, string.substr(ind,n), getc, fgetc, atoi, etc) I know I'm close but I can't find examples of exactly what I'm looking for.
The simplest and most direct way I've found is
stringstream convert(string1);
convert>>variable;
but it converts the whole string, if there was a way to add an ind position in it like
string1.substr(0,1)
that'd do the trick...
It is as simple as this:
std::string num = "94715";
size_t i = 0;
assert( num.length() > 4 );
int a = num[i++] - '0';
int b = num[i++] - '0';
int c = num[i++] - '0';
int d = num[i++] - '0';
int e = num[i++] - '0';
note: this may not work properly on systems not using ACII encoding, but it is unlikely you would hit such problem.
int number = 9544;
int vector[10]; // you can make it as big as you want or simply use std::vector
int position = 0;
while (number != 0)
{
vector[position++] = number % 10; // this assigns the last digit
number = number / 10; // remove the last digit
}
for(int i=0; i<position; i++)
std::cout << vector[i]; // this will print 4459
Now you have your number's digits inside an int vector.
This example uses chars because a string is still a char array.
This is for if you are on Windows. Just input and press the enter key.
Like Dieter Lücking suggested, it does this character by character.
It uses kbhit() and getch() found in conio.h to process single characters.
Then it multiplys the characters with appropiate position to get full integer.
And then it stores them in the array digitArray[ ], and assigns them to a,b,c,d or e after. Now you can do whatever with a,b,c,d and e.
Here is some code to demonstrate.
// Re: Now it is just clunky
// one ten hundred thousand
// 1 2 3 4
//digitn=digitArray[3]*1 + digitArray[2]*10 +digitArray[1]*100 +digitArray[0]*1000
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
int digit=0,digitInput=0;
int digitArray[10]={0},digitn;
int numberOfInputDigits=7;
void SplitIntoDigits(void);
int a=0,b=0,c=0,d=0,e=0,f=0;
int main(){
system("color 1F"); //Blue background
while(1){
cout<<"\nPlease enter number. Max Input is "<<numberOfInputDigits<<" individual digits \n";
cout<<"or press enter when done \n>" ;
memset(digitArray,0,sizeof(digitArray));
a=0;b=0;c=0;d=0;e=0;f=0;
SplitIntoDigits();
//cout<<"\n"<<digitArray[7]="<<digitArray[7];
cout<<"\n\n";
a = digitArray[0];
b = digitArray[1];
c = digitArray[2];
d = digitArray[3];
e = digitArray[4];
f = digitArray[5];
cout<<"a = "<< a <<"\n";
cout<<"b = "<< b <<"\n";
cout<<"c = "<< c <<"\n";
cout<<"d = "<< d <<"\n";
cout<<"e = "<< e <<"\n";
cout<<"f = "<< f <<"\n";
cout<<"\n\n The whole combined int number is "<<digitn<<"\n\n";
}
return 0;
}
/*********************************
* *
********************************/
void SplitIntoDigits(void){
digitArray[0]=0;
digitArray[1]=0;
digit=0;
digitInput=0;
while((digit<numberOfInputDigits)){
if (kbhit()){
digitInput=getch();
if (digitInput==27) exit(0);
if ((digitInput>47) && (digitInput<59)) {
digitArray[digit]=(unsigned char)digitInput-48;
digit++;
cout<<digitInput-48;
}
if (digitInput==13) { digitn=digitArray[0]; break; }
}
}
switch(digit) {
case 0:
case 1:
digitn=digitArray[0]*1 ;
break;
case 2:
digitn= digitArray[1]*1 +digitArray[0]*10 ;
break;
case 3:
digitn= digitArray[2]*1+digitArray[1]*10 +digitArray[0]*100 ;
break;
case 4:
digitn=digitArray[3]*1+digitArray[2]*10+digitArray[1]*100+digitArray[0]*1000 ;
break;
case 5:
digitn=digitArray[4]*1+digitArray[3]*10+digitArray[2]*100+digitArray[1]*1000+digitArray[0]*10000 ;
break;
case 6:
digitn=digitArray[5]*1+digitArray[4]*10+digitArray[3]*100+digitArray[2]*1000+digitArray[1]*10000
+digitArray[0]*100000;
break;
case 7:
digitn=digitArray[6]*1+digitArray[5]*10+digitArray[4]*100+digitArray[3]*1000+digitArray[2]*10000
+digitArray[1]*100000 +digitArray[0]*1000000;
break;
case 8:
digitn=digitArray[7]*1+digitArray[6]*10+digitArray[5]*100+digitArray[4]*1000+digitArray[3]*10000
+digitArray[2]*100000 +digitArray[1]*1000000+digitArray[0]*10000000;
break;
case 9:
digitn=digitArray[8]*1+digitArray[7]*10+digitArray[6]*100+digitArray[5]*1000+digitArray[4]*10000
+digitArray[3]*100000 +digitArray[2]*1000000+digitArray[1]*10000000 +digitArray[0]*100000000;
break;
}
// if (digitInput!=13) digitn=digitArray[3]*1+digitArray[2]*10+digitArray[1]*100+digitArray[0]*1000 ;
//cout<<("\n%i\n\n",digitn);
}
/*********************************
* *
********************************/

c++ Roman Additive Form Conversion

I'm working in a program that converts from Roman to Decimal. I have to validate 2 things: One that the characters entered are M or D or C or L or X or V or I, in other words valid for processing.
Number two, I have to make sure that bigger characters value go first and if not to print and error message and have the user to try again (this is the part where I am stuck)
For instance, If I wanted to input 9 and I input IX it should display an error message because is not in Additive form. It should be VIIII. How can I code this so it compares characters to know whether bigger letter values are first and so on?
I keep getting incorrect validation.
Is there a way to assign a value to the letters in the string? I'm thinking in comparing them as int values which I know how to and from there validate input format.
void RomanNum::setRomanNumber() //get input and calculate decimal equivalent
{
//I 1, V 5, X 10, L 50, C 100, D 500, M 1000
int value = 0;
string input;
char current, next;
enum validationData { M, D, C, L, X, V, I };
bool validationCharacters = true;
//bool validationAdditiveForm = true;
getline(cin, input, '\n');
for (int i = 0; i < input.length(); i++) //calculate each Roman letter at a time
{
current = input[i];
next = current + 1;
if (current >= validationData(next))
{
switch (input[i])
{
case 'M':
value += 1000;
break;
case 'D':
value += 500;
break;
case 'C':
value += 100;
break;
case 'L':
value += 50;
break;
case 'X':
value += 10;
break;
case 'V':
value += 5;
break;
case 'I':
value += 1;
break;
default:
validationCharacters = false;
break;
}
}
else
{
cout << "\nInvalid order. Bigger values go first\n";
}
}
}
I would recommend a std::map<char, int> to hold the mapping between letetrs and values.
With the map, you can then convert the input string (a sequence of characters) to a sequence of values (std::vector<int>). From there on, it's just a single check to see if the vector is sorted, and a single function call to add up all values. (I'll leave finding the right function as homework)

Write multiple things in a switch statement in C

So i need to have my switch statement go through and write out five two if the user says 52 but i cannot get it pass my 0-9. if they type 0-9 it works perfect but if i try to do any number past that it makes a blank. help!
#include <stdio.h>
int main (void)
{
int x;
printf("Please enter an integer: ");
scanf("%d", &x);
printf("\nYou have entered:\n\n");
for(x;x<0;x++);
switch (x)
{
case 0:
printf("zero");
break;
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
case 4:
printf("four");
break;
case 5:
printf("five");
break;
case 6:
printf("six");
break;
case 7:
printf("seven");
break;
case 8:
printf("eight");
break;
case 9:
printf("nine");
break;
}
printf("\n\n");
return 0;
}
do {
switch (x%10) {
...
}
x = x / 10;
} while (x>0) ;
or to get it in the right order use recursion
void f(int x) {
if (x==0) return;
f(x/10);
switch(x%10) { ... }
}
This question has been asked-and-answered before.
The for-loop you wrote is empty, because the body of the loop ends with the semi-colon:
for(x;x<0;x++) /* Empty Body!!*/ ;
The way a typical for loop works is:
for( /*Initialize*/; /*Test*/; /*Change*/)
{
/* Body */
}
In your case, I think you want:
for(int i=0; i < x; ++i)
{
switch(x)
{
[...]
}
}
This will:
Initialize i to 0
Test if i is LESS THAN x (the number you entered)
Keep increasing i by 1, until it gets up to x.
I'm not going to do your homework for you but consider the following pseudo-code:
print_text_digits(x)
{
if (x >= 10) print_text_digits(x / 10);
switch (x) {
print "zero" through "nine" as appropriate
}
}
main()
{
scan number into x;
print_text_digits(x);
}
This relies on a recursive routine so that you get your digits processed one at a time, with the might significant digit printed first.
You could solve this with recursion.
void printDigit(int x) {
int digit = x%10;
if(digit!=x)
printDigit(x/10);
switch(digit) {
...
}
}
This will print the most significant figure first, unlike the while loops most people are mentioning.
I believe you need this:
#include <stdio.h>
int main (void)
{
int count = 0;
int x, count2;
printf("Please enter an integer: ");
scanf("%d", &x);
printf("\nYou have entered:\n\n");
int aux = x;
while(aux>0) {
aux=aux/10;
count++;
}
count2 = count;
while(count) {
aux = x;
for(int i=count-1;i>0;i--)
aux=aux/10;
for(int i=count2-count;i>0;i--)
aux%=10;
switch (aux) {
case 0:
printf("zero");
break;
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
case 4:
printf("four");
break;
case 5:
printf("five");
break;
case 6:
printf("six");
break;
case 7:
printf("seven");
break;
case 8:
printf("eight");
break;
case 9:
printf("nine");
break;
}
count--;
if(count) printf(" ");
}
printf("\n\n");
return 0;
}
Now, with an input 52 it will propelly return five two.
#include <stdio.h>
static const char * const num[] = {
"zero ", "one ", "two " , "three ", "four ",
"five ", "six ", "seven ", "eight ", "nine "
};
void printNum(int x)
{
if (x < 10) {
printf(num[x]);
return;
}
printNum(x / 10);
printNum(x % 10);
}
int main (void)
{
int x;
printf("Please enter an integer: ");
scanf("%d", &x);
printf("\nYou have entered:\n\n");
printNum(x);
return 0;
}
Here's what you need to do. Similar to what #simonc said, it's a good idea to convert the user input to a string, then loop through the characters. For each character, convert it into an int and then take that into the switch statement.
EDIT---------------------------
Here's a way with strictly using integers.
First find how many digits your integer has. You can do this by a method mentioned here.
Then do integer division starting from the largest power of 10 that divides the integer you have and divide the divisor by 10 until you reach 1. For example:
If user input is 213, it has 3 digits. We divide this by 100 first.
213/100 = 2
We take the 2 and put it into the switch statement, outputting 2. Now we're done with the hundreds digit, so now we take the 13 from 213 and divide it by 10.
13/10 = 1 So now we output one.
Keep doing this process until you get to the ones digit.
Does this make sense?