I've been trying to get this code working, but somehow I can't do it..
#include <iostream.h>
#include <stdio.h>
int main() {
int a,b,c;
int y=3;
int i=2;
int g[] = {20};
int m,k;
int Z;
printf("Enter a number for a");
scanf("%d", &a);
printf("Enter a number for b");
scanf("%d", &b);
printf("Enter a number for c");
scanf("%d", &c);
m=y;
do
{
Z = a+b-c;
switch(Z)
{
case '0':
case '1':
k=17;
m+=b;
break;
case '2':
m+=b;
m=a;
break;
case '3':
m=a-c;
m+=b;
m=a;
break;
case '7':
m+=b;
break;
default:
m=a;
break;
}
g[m] = m%i;
m--;
}while(m>b);
}
This is the scheme that I had to turn into coding. http://ff.tu-sofia.bg/PIK/Izpiti/MidTest07.html
y and i are 3 and 2 by default, the array g should contain 20 integers, and the users have to type values for a, b and c.
Z is an integer and your cases are looking for strings. get rid of the quotes around the numbers in the cases.
The array g does not contain 20 integers it contains one element 20 in this case. I think what you meant was g[20] = {}
Also How is Z calculated ? Is it (a+b)-c or a+(b-c)? You need parentheses to make your intent clearer
Z = a+b-c;
switch(Z)
{
case 0:
case 1:
k=17;
m+=b;
break;
case 2:
m+=b;
m=a;
break;
Related
Here's another problem I have been working on. This is purely for educational purposes, and I understand that it is probably not the most efficient way to execute this objective.
The goal is to read a user's input in the console/terminal (are these synonymous?), and count the occurrence of the integer values 0, 1, 2, 3, and 4 using a switch-case construction.
As currently constructed, I'm storing the user input as a character value, and comparing each character to the integers in character form (e.g. '0', '1'...).
When I run my code, after entering an example such as 1235000, I get zero values for everything. However, if I initiate the input with a character, such as a000125, the program executes correctly.
What is going on here, and how should I resolve this?
#include <stdio.h>
using namespace std;
int main()
{
int zeros=0;
int ones=0;
int twos=0;
int threes=0;
int fours=0;
int others=0;
int num;
printf("please enter a positive integer\n ");
scanf_s("%d",&num);
while ((num = getchar()) != EOF)
{
switch (num)
{
case '0':
zeros++;
break;
case '1':
ones++;
break;
case '2':
twos++;
break;
case '3':
threes++;
break;
case '4':
fours++;
break;
default:
others++;
}
}
printf("the number of zeros is %d\n", zeros);
printf("the number of ones is %d\n", ones);
printf("the number of twos is %d\n", twos);
printf("the number of threes is %d\n", threes);
printf("the number of fours is %d\n", fours);
printf("other characters/numbers: %d\n", others);
return 0;
}
Here, I've made a function, that takes a character array and a single element array as input.
The input of expression is like "56+78", and then someone suggested this approach of using ascii code and for loops to store the two "numeric" substrings as two numbers, and used the character and switch statement below. But, I don't understand the part of storing these substrings as numbers and the asciicode concept.
void calculate(char ch[], char op[]){
int i;
int num1 = 0, num2 = 0;
for(i=0; ch[i]!='\0';i++)
{
if((int)ch[i]>=48 && (int)ch[i]<=57){
num1 = num1*10+(((int)ch[i])-48);
}
else{
op[0]=ch[i];
break;
}}
i++;
for(; ch[i]!='\0';i++)
{
if((int)ch[i] >= 48 && (int)ch[i] <= 57){
num2 = num2*10+(((int)ch[i])-48);
}
}
cout<<"OUTPUT: ";
switch(op[0])
{
case '+':
cout<<num1 + num2<<endl;
break;
case '-':
cout<<num1 - num2<<endl;
break;
case '*':
cout<<num1 * num2<<endl;
break;
case '/':
cout<<num1 / num2<<endl;
break;
}
}
poziomy= char;
pionowy= digit; ( no problems with this one)
So I need to convert char into a digit in function but obviusly I cannot do char=int, so I dont know how to pass on the converted char into digit properly.
I guees i can do two functions but maybe there is an easier way?
I thought of making a new variable poziomy_c but I dont know how to pass it to Ruch_gracza()
int Convert_digit (int cyfra)
{
switch (cyfra)
{
case 10: return 0;break;
case 9: return 1;break;
case 8: return 2;break;
case 7: return 3;break;
case 6: return 4;break;
case 5: return 5;break;
case 4: return 6;break;
case 3: return 7;break;
case 2: return 8;break;
case 1: return 9;break;
}
}
int Convert_letter (char literka)
{
switch (literka)
{
case 'A': return 0; break;
case 'B': return 1; break;
case 'C': return 2; break;
case 'D': return 3; break;
case 'E': return 4; break;
case 'F': return 5; break;
case 'G': return 6; break;
case 'H': return 7; break;
case 'I': return 8; break;
case 'J': return 9; break;
}
}
void Conwert(int &pionowy, char poziomy)
{
pionowy=Convert_digit(pionowy);
int poziomy_c;
poziomy_c=Convert_letter (poziomy);
}
void Ruch_gracza1 (int plansza[10][10])
{
int pionowy ;
char poziomy;
cout << "wprowadz wspolrzedne pola na ktorym lezy pion który chcesz ruszyc ( w pionie , potem w poziomie)" << endl;
cin >> pionowy >> poziomy;
Conwert (pionowy,poziomy);
cout << pionowy << endl;
cout << poziomy << endl;
}
You can use char arithmetic to make this a whole lot easier. Since 'A' to 'Z' will be contiguous in ASCII/Unicode, you can do literka - 'A' to get how far literka is from A (which is what your switch is doing):
int Convert_letter (char literka) {
if(!std::isalpha(literka)) { return literka; } // Not a letter
return std::toupper(literka) - 'A';
}
Or if you want a more robust solution to cover even less common character encodings:
int Convert_letter (char literka) {
if(!std::isalpha(literka)) { return literka; } // Not a letter
std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
return std::distance(std::begin(alphabet), std::find(std::begin(alphabet), std::end(alphabet), literka));;
}
Convert_digit will look similar (except with std::isdigit instead of std::isalpha).
You can do as
char c = 'B';
int digit = c - 'A';
return digit;
You need some knowledge about the ASCII table and data type in C++.
Simply, a char is an integer from -128 ... 127. If you declare a char variable name ch like this:
char ch = 'B';
C++ will understand that ch = 66 (look at ASCII table). So that we can do arithmetic operator with ch like an integer variable.
ch - 'A'; // result 1, because 'A' = 65
ch - 65; // same result with ch - 'A'
Finally, you can write your function like this:
int functionChar2Int(char x){
return x - 'A';
}
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?
Lets imagine I have got functions:
int switcherINT(char &c){
switch (c){
case '1': return 1; break;
case '2': return 2; break;
case '3': return 3; break;
case '4': return 4; break;
case '5': return 5; break;
case '6': return 6; break;
case '7': return 7; break;
case '8': return 8; break;
case '9': return 9; break;
case '0': return 0; break;
default: return err;
}
}
char switcherCHAR(int &c){
switch (c){
case 1: return '1'; break;
case 2: return '2'; break;
case 3: return '3'; break;
case 4: return '4'; break;
case 5: return '5'; break;
case 6: return '6'; break;
case 7: return '7'; break;
case 8: return '8'; break;
case 9: return '9'; break;
case 0: return '0'; break;
default: return errCH;
}
}
and I am trying to compute nest expression:
c.str[i] = switcherCHAR(switcherINT(pthis->str[pthis->size-i-1])-switcherINT(pb->str[pb->size-i-1])-loc);
where
longMath *pthis(this),*pb(&b);
longMath c;
class longMath{
protected:
char* str;
int size;
protected:
........
compiler says:
"can not convert parameter 1 from int into &int"
Haw can I solve this problem?
The expression that you've given as an argument to switcherCHAR gives you a temporary int. You cannot pass a temporary as a reference - unless you make the reference const. Just change switcherCHAR to take a const int& (and while you're at it, make switcherINT take a const char&). However, this are very simple types and you're probably better off just taking them by value. So change them to take just int and char.
Nonetheless, your functions are pretty strange. It is very easy to convert between a number x and it's char counterpart just by doing '0' + x. The numerical digit characters are guaranteed to be in consecutive order. So if you take the value of '0' and add, lets say, 5, you will get the value of the character '5'.
It would be much better to use functions like this:
int switcherINT(const char &c) {
return (c >= '0' && c <= '9') ? c - '0' : err;
}
char switcherCHAR(const int &c) {
return (c >= 0 && c <= 9) ? '0' + c : errCH;
}