When I compile this program, it outputs both the corresponding switch case and the default tag contents, the only value it doesn't print out for is January, any help would be great!
Sample Input: 4
Sample Output:
April
Error
Though I could make the program better, I need to have it this specific way.
while (month != EOF)
{
switch (month)
{
case 49:
month2 = getchar();
switch (month2)
{
case 10:
printf("January \n");
break;
case 48:
printf("October \n");
break;
case 49:
printf("November \n");
break;
case 50:
printf("December \n");
break;
}
break;
case 50:
printf("February \n");
break;
case 51:
printf("March \n");
break;
case 52:
printf("April \n");
break;
case 53:
printf("May \n");
break;
case 54:
printf("June \n");
break;
case 55:
printf("July \n");
break;
case 56:
printf("August \n");
break;
case 57:
printf("Septembe \n");
break;
default: printf("Error \n");
}
month = getchar();
}
system("PAUSE");
return (0);
}
You just need an extra case to handle the stray '\n' that comes from you pressing enter at your terminal. January should work if you just type 1
Edit:Actually I just tested it on Linux and it works flawlessly, there might be some minor differences on Windows, though.
while (month != EOF)
{
switch (month)
{
case 49:
month2 = getchar();
switch (month2)
{
case 10:
printf("January \n");
break;
case 48:
printf("October \n");
break;
case 49:
printf("November \n");
break;
case 50:
printf("December \n");
break;
}
break;
case 50:
printf("February \n");
break;
case 51:
printf("March \n");
break;
case 52:
printf("April \n");
break;
case 53:
printf("May \n");
break;
case 54:
printf("June \n");
break;
case 55:
printf("July \n");
break;
case 56:
printf("August \n");
break;
case 57:
printf("Septembe \n");
break;
// Filter out stray \n
case '\n':
break;
default: printf("Error \n");
}
month = getchar();
}
system("PAUSE");
return (0);
Related
I'm trying to create a map for a videogame with lncurses in C.
I use mvaddch() to print character to the console, but when i print the same char in a row consuctivly it prints only 2 char.
`do {
for(i=0;i<ROW;i++){
for(l=0;l<COLUMNS;l++){
switch(map[i][l]){
case 0:
mvaddch(i,l,ULCORNER);
break;
case 1:
mvaddch(i,l,URCORNER);
break;
case 2:
mvaddch(i,l,LLCORNER);
break;
case 3:
mvaddch(i,l,LRCORNER);
break;
case 4:
mvaddch(i,l,HLINE);
break;
case 5:
mvaddch(i,l,VLINE);
break;
case 6:
mvaddch(i,l,'a');
break;
case 8:
mvaddch(i,l,'=');
break;
default:
mvaddch(i,l,' ');
break;
}
}
}
refresh();
} while (true);`
Any suggest?
I'm trying to create a fiscal code calculator algorithm.
Here's the code:
#include<stdio.h>
#include<string.h>
int main()
{
int Day,Month,Year,i;
char Mo;
char Name[1][30];
char Surname[1][30];
char A,B,C,D,E,H,L,M,P,R,S,T;
printf("Insert your birthday day: ");
scanf("%d",&Day);
printf("Insert your birthday month: ");
scanf("%d",&Month);
printf("Insert your birthday year (last two numbers): ");
scanf("%d",&Year);
/*Month calculator*/
switch(Month)
{
case 1:
Mo="A";
break;
case 2:
Mo="B";
break;
case 3:
Mo="C";
break;
case 4:
Mo="D";
break;
case 5:
Mo="E";
break;
case 6:
Mo="H";
break;
case 7:
Mo="L";
break;
case 8:
Mo="M";
break;
case 9:
Mo="P";
break;
case 10:
Mo="R";
break;
case 11:
Mo="S";
break;
case 12:
Mo="T";
break;
}
printf("Your fiscal code is: %d%c%d",Year,Mo,Day);
}
In every case of the switch i receive the same error: Incompatible pointer to integer conversion assigning to 'char' from 'char[2]'.
Where is the error?
Thanks to all!
You are trying to assign chars to char*s. Mo is a char and strings surrounded in double quotes(") are char*s ending with a \0. Use single quotes(') to denote characters.
Change
switch(Month)
{
case 1:
Mo="A";
break;
case 2:
Mo="B";
break;
case 3:
Mo="C";
break;
case 4:
Mo="D";
break;
case 5:
Mo="E";
break;
case 6:
Mo="H";
break;
case 7:
Mo="L";
break;
case 8:
Mo="M";
break;
case 9:
Mo="P";
break;
case 10:
Mo="R";
break;
case 11:
Mo="S";
break;
case 12:
Mo="T";
break;
}
to
switch(Month)
{
case 1:
Mo='A';
break;
case 2:
Mo='B';
break;
case 3:
Mo='C';
break;
case 4:
Mo='D';
break;
case 5:
Mo='E';
break;
case 6:
Mo='H';
break;
case 7:
Mo='L';
break;
case 8:
Mo='M';
break;
case 9:
Mo='P';
break;
case 10:
Mo='R';
break;
case 11:
Mo='S';
break;
case 12:
Mo='T';
//break; Not needed
}
Here is a program to print Roman Numeral values up to a
hundred everything works fine except for when i==88, on this line tab indents one extra set to stop.
I sort of understand why that is 8 character.
How can I fix that ?
Thank you.
int main() {
unsigned int i, tensDigit, singleUnit;
printf( "Roman Numeral\t\t\t\tDecimal\n");
for ( i = 1; i <= 100; i++ ) {
tensDigit = i / 10;
singleUnit = i % 10;
switch ( tensDigit ) {
case 0:
break;
case 1:
printf("X");
break;
case 2:
printf("XX");
break;
case 3:
printf("XXX");
break;
case 4:
printf("XL");
break;
case 5:
printf("L");
break;
case 6:
printf("LX");
break;
case 7:
printf("LXX");
break;
case 8:
printf("LXXX");
break;
case 9:
printf("XC");
break;
case 10:
printf("C");
break;
default:
break;
}
switch ( singleUnit ) {
case 0:
printf("\t\t\t\t%d\n", i);
break;
case 1:
printf("I\t\t\t\t%d\n", i);
break;
case 2:
printf("II\t\t\t\t%d\n", i);
break;
case 3:
printf("III\t\t\t\t%d\n", i);
break;
case 4:
printf("IV\t\t\t\t%d\n", i);
break;
case 5:
printf("V\t\t\t\t%d\n", i);
break;
case 6:
printf("VI\t\t\t\t%d\n", i);
break;
case 7:
printf("VII\t\t\t\t%d\n", i);
break;
case 8:
printf("VIII\t\t\t\t%d\n", i);
break;
case 9:
printf("IX\t\t\t\t%d\n", i);
break;
}
}
return 0; }
First post in this awesome site!
So, I'm pretty much a dabbler in C language, and I am making a program that could return the day at the user-entered date. I didn't use any functions or structures, for I want to build my basic concepts in C first, which here are, if-else, switch statements, etc...
The program compiled quickly & ran w/o errors, but the answer is delayed by 1 day ;) I mean, when I entered '21 7 1993', which was a 'Wednesday', I'm getting 'Thursday'. Similarly for others.
I'm sure there's a fault in the concept. Please help me sort it out.Any comments welcome on the code below:
#include<stdio.h>
int temp,yr,yr_new,yr_latest,date,month,i,leap,ord,odd=0;
char flag='0';
int main()
{
clrscr();
puts("Enter the date in the format dd/month-no/yyyy");
scanf("%d %d %d",&date,&month,&yr);
temp=yr/1000;
switch(temp)
{
case 1: if(temp==0)
puts("ERROR");
case 2: if(temp==1)
{
if(yr<1600)
yr_new=yr-1200;
else
yr_new=yr-1600;
break;
}
case 3: if(temp==2)
{ if (yr<2400)
yr_new=yr-2000;
else if(yr<2800)
yr_new=yr-2400;
else
yr_new=yr-2800;
break;
}
}
temp=(yr_new/100);
odd+=(temp*5);
yr_latest=yr_new-(temp*100);
leap=yr_latest/4;
ord=yr_latest-leap;
for(i=1;i<=leap;i++)
odd+=2;
for(i=1;i<=ord;i++)
odd+=1;
/* Leap Year */
if(yr%400==0 && yr%100!=0)
flag='1';
/* month */
switch(month)
{
case 1:
{odd+=(date%7); break; }
case 2:
{odd+=(date%7);break;}
case 3:
{ odd+=((date%7)+3);
if(flag=='1')
odd+=1;
break ;
}
case 4:
{ odd+=((date%7)+6);
if(flag=='1')
odd+=1;
break; }
case 5:
{ odd+=((date%7)+8);
if (flag=='1')
odd+=1;
break;
}
case 6:
{ odd+=((date%7)+11);
if (flag=='1')
odd+=1;
break; }
case 7:
{ odd+=((date%7)+13);
if(flag=='1')
odd+=1;
break; }
case 8:
{ odd+=((date%7)+16);
if(flag=='1')
odd+=1;
break ;}
case 9:
{ odd+=((date%7)+19);
if(flag=='1')
odd+=1;
break;}
case 10:
{ odd+=((date%7)+21);
if(flag=='1')
odd+=1;
break;}
case 11: {
odd+=((date%7)+24);
if(flag=='1')
odd+=1;
break; }
case 12:
{ odd+=((date%7)+26);
if(flag=='1')
odd+=1;
break;
}
}
odd=odd%7;
switch(odd)
{ case 0:puts("Sunday"); break;
case 1:puts("Monday"); break;
case 2:puts("Tuesday"); break;
case 3:puts("wednesday"); break;
case 4:puts("thursday"); break;
case 5:puts("friday"); break;
case 6:puts("Saturday"); break;
default: puts("error!");
}
getch();
return 0;
}
I think you are having some problem in your switch case!. Try the following changes, It is some what different logic from yours-
#include<stdio.h>
int temp,yr,yr_new,yr_latest,date,month,i,leap,ord,odd=0;
int main()
{
puts("Enter the date in the format dd/month-no/yyyy");
scanf("%d %d %d",&date,&month,&yr);
temp=yr/1000;
switch(temp)
{
case 1: if(temp==0)
puts("ERROR");
case 2: if(temp==1)
{
if(yr<1600)
yr_new=yr-1200;
else
yr_new=yr-1600;
break;
}
case 3: if(temp==2)
{
if(yr == 2000) // Note this change also
yr_new=yr-1900;
else if (yr<2400)
yr_new=yr-2000;
else if(yr<2800)
yr_new=yr-2400;
else
yr_new=yr-2800;
break;
}
}
temp=(yr_new/100);
odd+=(temp*5);
yr_latest=yr_new-(temp*100);
yr_latest=yr_latest-1; // Here i am leaving the current year in odd days calculation.
leap=yr_latest/4;
ord=yr_latest-leap;
for(i=1;i<=leap;i++)
odd+=2;
for(i=1;i<=ord;i++)
odd+=1;
for(i=1;i<month;i++) // this logic is to calculate the odd days for the current year.
{
switch(i)
{
case 1:
odd+=3;
break;
case 2:
if((yr_latest+1)%4 == 0)
odd+=1;
else odd+=0;
break;
case 3:
odd+=3;
break;
case 4:
odd+=2;
break;
case 5:
odd+=3;
break;
case 6:
odd+=2;
break;
case 7:
odd+=3;
break;
case 8:
odd+=3;
break;
case 9:
odd+=2;
break;
case 10:
odd+=3;
break;
case 11:
odd+=2;
break;
case 12:
odd+=3;
break;
}
}
odd+=date;
odd=odd%7;
switch(odd)
{ case 0:puts("Sunday"); break;
case 1:puts("Monday"); break;
case 2:puts("Tuesday"); break;
case 3:puts("wednesday"); break;
case 4:puts("thursday"); break;
case 5:puts("friday"); break;
case 6:puts("Saturday"); break;
default: puts("error!");
}
return 0;
}
Current year means,if your input is '21 7 1993', first i am calculating odd days till '12.12.1992', then i am calculating the odd days for the remaining days!
newcomer to C here.
I seem to have run afoul on a problem that I have to do. The goal here is to make a program that writes out a two-digit number in words. Alternatively, the user can enter "Q" to quit. This is the bit that I am having trouble with. Can anyone point out what I am doing wrong?
#include <stdio.h>
int main(void)
{
int digit_one;
int digit_two;
enum state {fail, quit};
int status = fail;
printf("Enter a two-digit number or press Q to quit: ");
scanf("%1d%1d",&digit_one,&digit_two);
if(digit_one == 'Q'){
status = quit;
}
else {
if (digit_one == 1) {
switch(digit_two % 10) {
case 0: printf("You entered: Ten"); break;
case 1: printf("You entered: Eleven"); break;
case 2: printf("You entered: Twelve"); break;
case 3: printf("You entered: Thirteen"); break;
case 4: printf("You entered: Fourteen"); break;
case 5: printf("You entered: Fifteen"); break;
case 6: printf("You entered: Sixteen"); break;
case 7: printf("You entered: Seventeen"); break;
case 8: printf("You entered: Eighteen"); break;
case 9: printf("You entered: Ninteen"); break;
}
return 0;
}
switch(digit_one % 10) {
case 2: printf("You entered: Twenty-"); break;
case 3: printf("You entered: Thirty-"); break;
case 4: printf("You entered: Forty-"); break;
case 5: printf("You entered: Fifty-"); break;
case 6: printf("You entered: Sixty-"); break;
case 7: printf("You entered: Seventy-"); break;
case 8: printf("You entered: Eighty-"); break;
case 9: printf("You entered: Ninety-"); break;
}
switch(digit_two % 10) {
case 0: 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;
}
return 0;
}
}
You can't read Q into an integer variable. Read the input into a character one.
Problem is that your scanf reads a string and converts it to integer immediately (thats what %d does).
You need to first read only a string (with %s or similar) to a buffer and then check that it is not 'Q'. After that you can do the conversion to integer with sscanf.
See http://en.cppreference.com/w/c/io/fscanf
Edit: Also, your code completely ignores the quit status.