Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to do switch then if else in it. I don't know where I missed but this doesn't work.
Here's my code:
include<stdio.h>
include<conio.h>
int main()
{
clrscr();
int grade;
printf("Input Grade");
scanf("%d",&grade);
switch(grade<101)
{
case 1:
if (grade>=95)
printf("A+");
else
printf("Invalid");
break;
case 2:
if (grade>=85)
printf("A");
else
printf("Invalid");
break;
default:
printf("Invalid");
}
getch();
}
So I don't know what's the matter. Only the first case work, when i enter a lower number than 95 'A' isn't the result. Please help me out.
If you have to use switch you should bring the if else statements outside of your switch in order to switch over a proper integer value. Something like this:
int state = 0;
if ( grade < 101 && grade >= 95 )
state = 1;
else if ( grade < 101 && grade >= 85 )
state = 2;
switch state {
case 1:
printf("A+");
break;
case 2:
printf("A");
break;
default:
printf("invalid");
break;
}
You can express this with switch statements in the following way (though you probably do not want to):
switch(grade)
{
case 100:
case 99:
case 98:
case 97:
case 96:
case 95:
printf("A+");
break;
case 94:
case 93:
case 92:
case 91:
case 90:
case 89:
case 88:
case 87:
case 86:
case 85:
printf("A");
break;
default:
printf("Invalid");
//or an if statement if more options are required.
break;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
int i=0, marks,pass=0, fail=0;
int A_Plus=0,A=0,A_minus=0,B_Plus=0,B =0,B_minus=0,C_Plus=0,C=0,C_minus=0,D_Plus=0,D=0,F=0;
while(marks !=-1)
{
scanf("%d", &marks);
if(marks<-1 || marks>100)
{
printf("You enter invalid mark: %d \n",marks);
}
if (marks != -1 &&(marks>=0 && marks <=100))
{
if(marks < 60)
{
fail = fail + 1;
}
else
{
pass = pass + 1;
}
i=i+1;
}
}
Although I do not recommend using a switch statement for "yes/no"-type decisions, such as yours, you could convert your if ... else blocks into the following switch block:
switch (marks/60) // If marks is less than 60, this expression will truncate to zero ...
{
case 0:
++fail;
break;
default: // ... otherwise, we'll get here.
++pass;
break;
}
Note that this assumes that you have previously ruled out negative numbers (as you have done).
This approach (dividing the 'switch variable' by a constant) can be useful when you have more than two possible outcomes, determined by adjacent, fixed-size ranges. For example, rather than just "fail" and "pass", you may have 4 different grades (ranges 0..29, 30..59, 60..89 and 90+); in such a case, a switch such as the following may be a good approach:
switch (marks/30)
{
case 0: // < 30
++fail;
break;
case 1: // 30 thru 59
++poor;
break;
case 2: // 60 thru 89
++good;
break;
default: // >= 90
++excellent;
break;
}
I assume this is some academic exercise rather than some useful design aim? So I'll play the silly game. Expanding on Adrian Mole's idea:
// Grade: 0 = fail
// 1 = pass
// other = invalid input
int grade == marks > 100 ?
-marks :
marks /= 60 ;
switch( grade )
{
case 0: fail++ ; break ;
case 1: pass++ ; break ;
default:
if( marks != -1) printf( "You enter invalid mark: %d \n", marks ) ;
}
Note in any case your original code can be simplified in any case:
if( marks == -1 ) // do nothing
else if( marks < -1 || marks > 100 ) printf( "You enter invalid mark: %d \n", marks ) ;
else if( marks < 60) fail++ ;
else pass++ ;
An "at face value" solution:
switch( marks )
{
case -1 : break
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:
case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19:
case 20: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29:
case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37: case 38: case 39:
case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49:
case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59:
fail++ ; break ;
case 60: case 61: case 62: case 63: case 64: case 65: case 66: case 67: case 68: case 69:
case 70: case 71: case 72: case 73: case 74: case 75: case 76: case 77: case 78: case 79:
case 80: case 81: case 82: case 83: case 84: case 85: case 86: case 87: case 88: case 89:
case 90: case 91: case 92: case 93: case 94: case 95: case 96: case 97: case 98: case 100:
pass++ ; break ;
default:
printf( "You enter invalid mark: %d \n", marks ) ;
}
With this latter solution at least you could insert the grade breaks that the Plus_A, A variables hint at perhaps. If you were to do that you should calculate the pass/fail count by adding the grade counts rather then incrementing for each grade case.
Moreover if you are to have multiple grades, do not have a separate variable for each one - use an array, then an enumeration to index the array:
int grade_count[12] = {0} ;
enum eGrade
{
A_Plus, A, A_minus,
B_Plus, B, B_minus,
C_Plus, C, C_minus,
D_Plus, D, F
} ;
Then you might have:
grade_count[B_Plus]++ ;
If you use gcc like compiler (gcc, clang etc) you can use the extension:
switch(marks)
{
case 0 ... 59:
fail = fail + 1;
break;
default:
pass = pass + 1;
break;
}
But it is not standard C
You have only two cases (greater or less than 60), so a switch would just be more complex. If you have a case with more (e.g. A, B, C etc. letter grades), you'd have to map [0, 100] to [A, B, C, D, F] to give a code or an enum, then switch on that result. Making the mapping would be more complex than just using a series of if / else if / else statements, but if you want to do that anyway, a loop like this would work:
enum GRADE {
GRADE_A,
GRADE_B,
...
GRADE_F,
GRADE_COUNT, // Not an actual grade.
};
...
GRADE grade = GRADE_F; // default
int mark = /* comes from somewhere */
struct {
GRADE grade;
int lowest;
} grade_list[] = {
{ GRADE_A, 90 },
{ GRADE_B, 80 },
{ GRADE_C, 70 },
{ GRADE_D, 60 },
{ GRADE_F, 0 },
};
for (int grade_idx = 0;
grade_idx < GRADE_COUNT;
grade_idx++
) {
if (marks >= grade_list[grade_idx].lowest) {
grade = grade_list[grade_idx].grade
break;
}
}
switch (grade) {
case GRADE_A:
...
break;
case ...:
...
break;
case GRADE_F:
...
break;
}
Don't do this as-is, instead collect the total count and subtract off the failures or successes later
// reject < 0 or > 100
...
marks_total++;
if (mark - 60 > 0) marks_passing++;
}
marks_failed = marks_total - marks_passing;
I'm getting this date as invalid "006.010.002021" and my question is, how can I validate it and convert it to "06.10.2021"
thank you in advance
int isDateValid(sDate date)
{
int daysPerMonth;
switch (date.month)
{
case 1:
case 2:
daysPerMonth = 28;
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
daysPerMonth = 30;
break;
case 12:
daysPerMonth = 31;
break;
default:
return 0;
}
if ((daysPerMonth == 28) && isLeapYear(date.year))
daysPerMonth++;
if (date.day > daysPerMonth || date.day <= 0 || date.day > 2117 || date.year < 1917)
return 0;
}
You can use strptime() to read a string into a date/time format. However, you can only check if the string failed or not, it will not tell you what is wrong and how to fix it.
The best way to handle a wrong date is to throw an error informing the user that the date is wrong, because your code can not fix it.
Your case 1: will assign 28 days to january as well and the other cases will also yield wrong days per month.
switch (date.month)
{
case 2:
daysPerMonth = 28;
break;
case 4:
case 6:
case 9:
case 11:
daysPerMonth = 30;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysPerMonth = 31;
break;
default:
return 0;
}
We've been given a assignment at school and the
while loop at the end of the code doesn't seem to give the value I expected.
#include <stdio.h>
#include <string.h>
int main() {
char str[50],storage[50][50]={0},temp[50]={0};
printf("Input Formula without space:");
gets(str);
int x=0, y=0,z=0,w,i,top=-1,max=strlen(str);
for(i=0;i<max;i++){
printf("Eyyyyyy");
top+=1;
switch(str[top]){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '-':
case '+':
case '*':
case '/':
case '9':storage[x][y]=str[top];x++;break;
case ')':
if (x-3<0)
break;
else{
while(storage[x-3][y]!=0){
temp[y]=storage[x-3][y];
y++;
printf("Hey");};
storage[x-3][y]='(';
while (temp[y]!=0){
storage[x-3][y+1]=temp[y];
y++;
};
for (z=2;z>=0;z--){
while (storage[x-z][y]!=0){
w=strlen(storage[x-3]);
storage[x-3][w]=storage[x-z][y];
}
}
}
x-=3;
break;
}
}
for(i=0;i<20;i++){
printf("%c",storage[0][i]);
}
for(i=0;i<20:i++){
printf("%c",storage[0][i]);
}
while(storage[x][y]!=0){
printf("%c",storage[0][y]);
y++;
}
}
I'd like to ask for some help on how do I make the while loop work. It doesn't give me any errors when compiling its just the value on while loop doesn't show up. only the other printfs before that.
regarding the while() statement at the end of the posted code:
while(storage[x][y]!=0){`
what are the initial values of x and y?
Since those values are already 'maxed out' the selection of storage[x][y] is past the end of the array `storage'
Suggest properly initializing those index values before entering the while() loop
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!
I want to create a program that requests from the user 10 grades and then filters them to pass and fail, then prints the number of passes and fails. I did the program but the output is wrong.
int pass,fail,grade,studentcounter;
pass=0;
fail=0;
grade=0;
studentcounter=10;
while (studentcounter!=0)
{
printf("enter the next grade\n");
scanf("%d",grade);
student--;
}
switch (grade)
{
case 1:
if (grade >= 50)
pass++;
break;
case 2:
if (grade <= 49)
fail++;
break;
}
}
printf("the number of fail is %d",fail);
printf("the number of pass is %d",pass);
}
The problem is that the program request the ten grades but at the end it will print the number of fail and the number of pass as zero. Why?
You are entering a grade number. A switch statement tests that grade against the cases, and i am pretty sure the grades are not 1 percent or 2 percent. An if statement would be a more logical choice in this situation.
Second of all, you have a code block which is never used. First you set studentcounter to zero, then you say "only execute this block when studentcounter is NOT zero"...
studentcounter=0;
while (studentcounter!=0) {
printf("enter the next grade\n");
scanf("%d",grade);
student--;
}
The third problem is, you misspelled grade.
you can rewrite your code as follows:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int pass,fail,grade,studentcounter;
pass=0;
fail=0;
grade=0;
studentcounter=0;
while (studentcounter < 10) {
printf("enter the next grade:\n");
scanf("%d",&grade);
if (grade >= 50) {
pass++;
} else {
fail++;
}
studentcounter++;
}
printf("the number of fail is: %d \n",fail);
printf("the number of pass is: %d \n",pass);
return 0;
}
Sorry if I overlooked something; I don't have time to throw it into my editor :P
cheers
you need to keep the count of pass/fails inside the while loop, the grade variable will be overwritten at every input.
edit:
also, dont use a switch statement.
Ok, the code as presented is a pile of crap. I will leave it as an exercise for you to sort out what I fixed for you:
#include <stdio.h>
int main(int argc, char *argv[])
{
int pass = 0;
int fail = 0;
int grade= 0;
int studentcounter;
for (studentcounter=1; studentcounter<=10; studentcounter++ )
{
printf("Enter grade for student #%-2d :", studentcounter);
scanf("%d",&grade);
if(grade >=50)
pass++;
if(grade<=49)
fail++;
}
printf("the number of fail is %d\n",fail);
printf("the number of pass is %d\n",pass);
}
PS. If this is homework, please give credit when you turn it in. You can learn a lot by looking at other people's code, but never claim to create that which you haven't.
hmm, since you have to use swicth i will suggest the following:
Keep a variable which checks whether the grade is more than 50 or not and then use this variable inside switch:
char pass_fail = (grade >= 50)? y : n;
switch( pass_fail )
{
case 'y':
// print pass
break;
case 'n':
// print fail
break;
}
hope this helps you
BTW, there is a solution that uses 'switch' in a reasonable fashion. I won't get into a discussion of the advantages/pitfalls of this approach or the arcaneness of the 'C' language, but just to prove it's possible, here it is:
#include <stdio.h>
int main(void)
{
int pass, fail, grade, answer, studentcounter;
pass = 0;
fail = 0;
grade = 0;
studentcounter = 10;
while ( studentcounter-- )
{
printf("enter the next grade\n");
scanf("%d", &grade);
answer = ( grade > 49 );
switch ( answer )
{
case 1:
pass++;
break;
default:
fail++;
break;
}
}
printf("the number of fail is %d \n", fail);
printf("the number of pass is %d \n", pass);
}
C 'case' statements do not work the way you are expecting.
They operate on 'constant-expressions' which means that in order to accomplish the task you have given, 'Determine if a grade is pass or fail' one would have to write a convoluted switch statement of the form
switch ( grade )
{
case 100:
case 99:
case 98:
case 97:
case 96:
case 95:
case 94:
case 93:
case 92:
case 91:
case 90:
case 89:
case 88:
case 87:
case 86:
case 85:
case 84:
case 83:
case 82:
case 81:
case 80:
case 79:
case 78:
case 77:
case 76:
case 75:
case 74:
case 73:
case 72:
case 71:
case 70:
case 69:
case 68:
case 67:
case 66:
case 65:
case 64:
case 63:
case 62:
case 61:
case 60:
case 59:
case 58:
case 57:
case 56:
case 55:
case 54:
case 53:
case 52:
case 51:
case 50:
pass++;
break;
default:
fail++;
break;
}
This would probably give you the correct answer, however, you may receive a 'fail' on your homework for writing such a monster.
I would try to think of another problem that has 'while, switch and if' statements or perhaps you could modify your current program such that, instead of accepting 10 grades, it prompted the user if they wished to enter another grade and accepted only the answer 'Y', 'y', or 'N', 'n' thus 'switching' on that answer to continue.
2 errors:
1: The while loop should end after the switch statement.
2: Your variable name is "studentcounter" but you are decrementing "student".
Check the following code. This should work.
void main()
{
int pass,fail,grade,studentcounter;
pass=0;
fail=0;
grade=0;
studentcounter=10;
while (studentcounter!=0)
{
printf("enter the next grade\n");
scanf("%d",grade);
studentcounter--;
switch (grade)
{
case 1:
if (grade >= 50)
pass++;
break;
case 2:
if (grade <= 49)
fail++;
break;
}
}
printf("the number of fail is %d",fail);
printf("the number of pass is %d",pass);
}