I'm building a program using a grade report and I'm having trouble calculating my GPA using a switch case. I'm unsure why it isn't assigning the correct values. I would also like if there is a way to ask for the number of classes taken and then get the loop to perform that said number of times.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Grades
{
char Name[20];
int Hrs;
int ID;
char ClassName[20];
char Grade;
char ClassID[6];
};
int main ()
{
struct Grades Transcript[6];
int classCnt = 0;
int vHrs=0, vGPA=0, totalHours=0, totalPoints = 0;
char vGrade;
char vName[20], vCID[6], vClassName[20];
printf("Enter Students Name: ");
fgets(vName, 20, stdin);
do
{ printf("\nEnter Class ID: ");
fgets(vCID, 6, stdin);
fflush(stdin);
strcpy_s(Transcript[classCnt].ClassID, vCID);
printf("Enter Class Name: ");
fgets(vClassName, 20, stdin);
strcpy_s(Transcript[classCnt].ClassName, vClassName);
printf("Enter Class Hours: ");
fflush(stdin);
scanf("%d", &vHrs);
Transcript[classCnt].Hrs = vHrs;
printf("Enter Class Grade: ");
fflush(stdin);
scanf("%c", &vGrade);
Transcript[classCnt].Grade = vGrade;
classCnt++;
fflush(stdin);
totalHours+=vHrs;
switch (vGrade) {
case 'A':
case 'a': 4*vHrs;
break;
case 'B':
case 'b': 3*vHrs;
break;
case 'C':
case 'c': 2*vHrs;
break;
case 'D':
case 'd': 1*vHrs;
break;
case 'F':
case 'f': 0;
break;
default: printf("Invalid Grade");}
totalPoints += vGrade;
vGPA = (totalPoints/totalHours);
}while(classCnt<=5);
printf("********************************** Grade Report: *************************************");
printf("\n%d\n", totalHours);
printf("%d\n", vGPA);
system("Pause");
return 0;
The expression statement:
4*vHrs;
is certainly valid in C but it doesn't actually do anything (a).
Perhaps you may want to assign it to something, such as with:
addPoints = 4 * vHrs;
(declaring addPoints beforehand, of course) and then use that to affect totalPoints later:
totalPoints += addPoints;
In terms of asking for a class count, you can use scanf("%d",...) to get an integer from the user and then just use that integer in a loop:
#include <stdio.h>
int main (void) {
int num, count;
printf ("Enter countdown value: ");
scanf ("%d", &count);
for (num = count; num > 0; num--)
printf ("%d ", num);
puts ("BLAST OFF");
return 0;
}
A sample run being:
Enter countdown value: 10
10 9 8 7 6 5 4 3 2 1 BLAST OFF
(a) Even a statement like 42; is valid, though useless. The reason this is allowed is because you can have side effects in expressions. The classic case, though not many learners immediately see this, is the venerable i++;.
This is an expression which gives you the current value of i (which you throw away unless you're using it somehow), then increments i as a side effect.
case 'A':
case 'a': 4*vHrs;
break;
case 'B':
case 'b': 3*vHrs;
break;
case 'C':
case 'c': 2*vHrs;
break;
case 'D':
case 'd': 1*vHrs;
break;
case 'F':
case 'f': 0;
None of these lines have any effect on the program you've written. You might want to assign 3*vHrs, 4*vHrs etc to a variable and then do the calculations below. You probably meant vHrs *= 3 or vHrs *=4 or something like that?
Related
I've attempted to find if the user has inputted a product id value that's a duplicate and if so, it just tells them that it's a duplicate value and then returns to the menu in my switch statement.
The actual result i get, is that after "productsfilled == 0", it won't utilise the For Loops to check for the duplicates and productsfilled will remain at 1. I've looked online and this way of finding duplicates tends to work and i have used it previously in my code, so I don't think that could be the issue.
Here's my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <conio.h>
int productsfilled;
struct ProductData{
int product_id;
char product_name[120];
int price;
};
int quiz_5(){
char ch;
int size, input = 0;
struct ProductData products[20];
while(1){
printf("\nWelcome To The Super Mall's Product List. Please Select An Option:\n1. Add Product\n2. Display Product\n3. Delete Product\n");
fflush(stdin);
switch(getchar()){
case '1':
printf("\nPlease Enter Product ID:");
scanf("%d",&products[productsfilled].product_id);
printf("\nPlease Enter Product Name:");
scanf("%s",&products[productsfilled].product_name);
printf("\nPlease Enter Product Price:");
scanf("%d",&products[productsfilled].price);
printf("Productsfilled: %d",productsfilled);
if(productsfilled == 0){
productsfilled = 1;
break;
}
for(int i = 0; i < productsfilled;i++){
for (int j = i + 1; j < productsfilled;j++){
if(products[i].product_id == products[j].product_id){
printf("\nPlease Use Different Product ID");
break;
}else{
printf("test");
productsfilled += 1;
break;
}
}
}
break;
case '2':
while(1){
for(int i = 0;i < productsfilled;i++){
printf("Product ID: %d Product Name: %s Product Price: %d\n",products[i].product_id,products[i].product_name,products[i].price);
}
printf("Please Press Enter To Continue");
fflush(stdin);
if(getchar() == '\n'){
break;
}
}
case '3':
break;
case '\n':
break;
default:
printf("Please Select An Option:\n1. Add Product\n2. Display Product\n3. Delete Product: ");
}
}
}
int main() {
int input = 1;
printf("Welcome to my assignment. Which quiz do you want to run (please input the number of the quiz e.g. for quiz 1, type 1): \n-Quiz 1\n-Quiz 2\n-Quiz 3\n-Quiz 4\n-Quiz 5\n-Quiz 6\n-Quiz 7\n");
while(input == 1){
fflush(stdin);
switch(getchar()){
case '5':
quiz_5();
break;
case '\n':
printf("Welcome to my assignment. Which quiz do you want to run (please input the number of the quiz e.g. for quiz 1, type 1): \n-Quiz 1\n-Quiz 2\n-Quiz 3\n-Quiz 4\n-Quiz 5\n-Quiz 6\n-Quiz 7\n");
getchar();
default:
printf("Invalid Input\n");
} }
return 0;
}
The problem is that you don't increment productsfilled before you enter the loop...therefore, productsfilled is always 1 less than the actual length of your array which means that you don't compare all elements in the array.
Try your program on 2 inputs, both with the same ID. You'll see that you don't compare anything.
You are wrong when using scanf for string input:
scanf("%s",&products[productsfilled].product_name);
You should not use &, you should use as below:
scanf("%119s",products[productsfilled].product_name);
OT, in main function:
switch(getchar()){
case '5':
...
Because getchar() will return int value, so if you want to access to quiz_5, you have to type 35 (ANSCI code) instead of type 5 when you run your program.
char a = '5';
similar to:
int a = 35;
I am a totally newbie about C programming. so my program is very long, sorry.
my professor wants to have a number system- binary to decimal, decimal to binary, octal to decimal, hexadecimal to binary. he also want to have a loop( if he wants to exit press [E], if not then press any key). Now i'm having a problem with this hexadecimal because it keeps saying " type mismatch in redeclaration" and i don't know now how to solve this problem.
so heres my not yet finished program because of "hexadecimal" problem. help me with this error. don't mind the octal to decimal, I am currently programming it :)
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 1000
long num, decimal(long), octal(long), binary(long),j;
char hexadecimal(char), k[MAX];
main()
{
char choice;
clrscr();
printf("[B]inary to Decimal\n");
printf("[D]ecimal to Binary\n");
printf("[O]ctal to Decimal\n");
printf("[H]exadecimal to Binary\n");
printf("[E]xit\n");
printf(" Enter your choice....");
choice=getche();
switch(choice)
{
case 'b':
case 'B': binary(j); break;
case 'd':
case 'D': decimal(num); break;
case 'o':
case 'O':
case 'h':
case 'H': hexadecimal(k[MAX]); break;
case 'e':
case 'E': return 0;
default: printf("\n Invalid choice.... press any key to REPEAT");
getch();
main();
}
printf("\nDo you want to [E]xit?");
choice=getch();
switch(choice)
{
case 'e':
case 'E': printf("\nInvalid choice... press any key to repeat");
getch();
main();
}
getch();
return 0;
}
long binary(long j)
{
long binary_val,decimal_val=0, base=1, rem;
printf("Enter a binary number( 1s & 0s): ");
scanf("%ld",&j);
binary_val=j;
while(j>0)
{
rem=j % 10;
decimal_val=decimal_val + rem * base;
j= j/ 10;
base=base * 2;
}
printf(" The Binary Number is %ld\n",binary_val);
printf(" Its decimal equivalent is = %d\n",decimal_val);
}
long decimal(long num)
{
long decimal_num, remainder, base=1, binary=0;
printf(" \nEnter a decimal integer: ");
scanf("%ld",&num);
decimal_num=num;
while(num>0)
{
remainder= num % 2;
binary=binary + remainder * base;
num=num/2;
base= base * 10;
}
printf(" Input number is %d\n",decimal_num);
printf(" Its binary equivalent is = %ld",binary);
}
char hexadecimal(char k[MAX])
{
long int i=0;
clrscr();
printf(" Enter any Hexadecimal number: ");
scanf("%s",&k);
printf("\n Equivalent binary value: ");
while(k[i])
{
switch(k[i])
{
case '0': printf("0000"); break;
case '1': printf("0001"); break;
case '2': printf("0010"); break;
case '3': printf("0011"); break;
case '4': printf("0100"); break;
case '5': printf("0101"); break;
case '6': printf("0110"); break;
case '7': printf("0111"); break;
case '8': printf("1000"); break;
case '9': printf("1001"); break;
case 'a':
case 'A': printf("1010"); break;
case 'b':
case 'B': printf("1011"); break;
case 'c':
case 'C': printf("1100"); break;
case 'd':
case 'D': printf("1101"); break;
case 'e':
case 'E': printf("1110"); break;
case 'f':
case 'F': printf("1111"); break;
default: printf("\n Invalid hexadecimal digit %c",k[i]); return 0;
}
i++;
}
}
The error you are getting type mismatch in redeclaration of hexadecimalis a result of the difference between the function you prototyped and implemented.
Your prototype is:
char hexadecimal(char), k[MAX];
This line prototypes a function hexadecimal that returns a char and takes a char as an argument AND this line also declares a global char array k of size MAX.
Your actual function is:
char hexadecimal(char k[MAX])
This function is a function that returns a char, but instead of taking a char like your prototype it instead takes a char array of size MAX. As you can see the prototyped function and the function itself are not the same. By making the functions exactly the same you will fix your issue.
To be honest, you don't need to pass anything into that function nor make a global char array as you can locally hold the array based on your code. The only other time you use the array you just pass it to this function which means it is better of as a local to that function anyway. So, you can simply do this:
char hexadecimal(void)
{
char k[MAX]
//same code below...
Now the function takes no arguments and k is still declared in the function, but is local instead of global. The prototype for this function would simply be:
char hexadecimal(void);
I have been able to do switch case program but I want program to run again and again until a user selects to quit.
I basically wants program to run again and again using do while loop...
switch(I)
{
case 1:
printf("67");
break;
case 2:
printf("45");
break;
default:
printf("default");
}
Use a do...while loop like this:
int I = 1; //Initialize to some non-zero number to prevent UB
printf("Enter 0 to quit \n");
do{
if (scanf("%d",&I) != 1) //If invalid data such as characters are inputted
{
scanf("%*[^\n]");
scanf("%*c"); //Clear the stdin
}
} while(I!=0); //Loop until `I` is not 0
This piece of code will loop until the user enters 0. You can change this code according to your needs. If you want your switch in this, copy your posted code after the scanf.
The loop will run until you enter -1 as input.
#include<stdio.h>
int main()
{
int I;
do
{
puts("Enter -1 to quit");
printf("Enter your choice: ");
scanf("%d",&I);
switch(I)
{
case 1:
printf("67\n");
break;
case 2:
printf("45\n");
break;
case -1:
puts("Bye");
break;
default:
printf("default\n");
}
}while(I != -1);
return 0;
}
this program runs untill user gives input 0 or a negative number...
#include<stdio.h>
int main()
{
int I;
do
{
scanf("%d",&I);
switch(I)
{
case 1:
printf("67");
break;
case 2:
printf("45");
break;
default:
printf("default");
}
}
while(I>0);
return 0;
}
Simple Use of Do-While Loop.
Choice is the variable in which user's choice will be stored, whether he wants to print the statement again or not.
int choice;
do{
printf("\nHello World!"); //This is the task of the program (Replace it with your task)
printf("\nDo You Want to Print it again ? 1 Yes/0 No: ");
scanf("%d",&choice);
}while(choice==1); //Loop will exit when choice gets value other than 1
// here switch will run until A is not equal to S
int N;
char A;
do{
cin>>N;
N = N%7;
cout<<endl;
cin>>A;
switch(N)
{
case 1: cout<<"Monday"<<endl; break;
case 2: cout<<"Tuesday"<<endl; break;
case 3: cout<<"Wednesday"<<endl; break;
case 4: cout<<"Thursday"<<endl; break;
case 5: cout<<"Friday"<<endl; break;
case 6: cout<<"Saturaday"<<endl; break;
case 0: cout<<"Sunday"<<endl; break;
default: cout<<"Invalid Input"; }}
while(A!='S');
I want to delete the contents of an int variable when it gets to an else statement.
The program requests a number between 1 and 5 using scanf and the number is stored in the int variable and if the number isn't between 1 and 5 then the user is directed to an else statement and I have used a goto statement to take it back to the start and I was wondering how I removed the contents of the variable during the else statement so I don't create a continuos loop.
With getchar it's fpurge(stdin). I'm running Mac OS X.
BELOW IS THE CODE:
#include <stdio.h>
int main (int argc, const char * argv[])
{
int code;
start:
puts("Please type your error code.");
puts("Range 1-5: ");
scanf("%d", &code);
switch(code)
{
case 1:
printf("INFORMATION\n");
case 2:
printf("INFORMATION\n");
case 3:
printf("INFORMATION\n");
case 4:
printf("INFORMATION\n");
case 5:
printf("INFORMATION\n");
default:
printf("INFORMATION\n");
goto start;
}
}
Just set the int value to something else, eg:
theValue = 0;
You are probably looking for a do...while loop
Edit Don't forget your break statements!!
do
{
puts("Please type your error code.");
puts("Range 1-5: ");
scanf("%d", &code);
switch(code)
{
case 1:
printf("INFORMATION\n");
break;
case 2:
printf("INFORMATION\n");
break;
case 3:
printf("INFORMATION\n");
break;
case 4:
printf("INFORMATION\n");
break;
case 5:
printf("INFORMATION\n");
break;
default:
printf("INVALID CODE\n");break;
}
} while(code<1 || code> 5);
I can't seem to get this program to compile.
I keep getting the error:
'Ammonia' undeclared 'Carbon_Monoxide' undeclared
and so on. Am I using the right function with switch?
/*This program will report the content of a compressed-gas cylinder based on the first letter of the cylinder's color.*/
#include <stdio.h>
int main (void)
{
int x;
char o, b, y, g;
int pause;
o = Ammonia;
b = Carbon_Monoxide;
y = Hydrogen;
g = Oxygen;
printf(" Enter a character representing the observed color of the cylinder \n" );
scanf("%d", &x);
switch (x)
{
case 'o': printf("The content is Ammonia\n");
case 'b': printf("The content is Carbon Monoxide\n");
case 'y': printf("The content is Hydrogen\n");
case 'g': printf("The content is Oxygen\n");
default: printf("Character out of range \n");
}
printf("After Switch \n");
printf("Enter new character to continue \n");
scanf("%d", &pause);
return 0;
}
It has nothing to do with your switch statement, but you will probably have to puzzle over the meaning of these four lines:
o = Ammonia;
b = Carbon_Monoxide;
y = Hydrogen;
g = Oxygen;
You don't use the variables thus defined anywhere, and the symbols "Ammonia", "Carbon_Monoxide" and so on are not defined - this is the cause of the error you are seeing.
Since this is homework, I don't want to give you the answer straight, but look at what you're doing with those chars (o, b, y, g) and ask yourself if it makes sense.
Also, on the switch statement, I'm pretty sure you need a break; after each case, else it will print each case's statement
try:
#include <stdio.h>
int main (void)
{
char x;
int pause;
printf(" Enter a character representing the observed color of the cylinder \n" );
scanf("%c", &x);
while(x){
switch (x)
{
case 'o': printf("The content is Ammonia\n"); break;
case 'b': printf("The content is Carbon Monoxide\n"); break;
case 'y': printf("The content is Hydrogen\n"); break;
case 'g': printf("The content is Oxygen\n"); break;
default: printf("Character out of range \n");
}
printf("After Switch \n");
printf("Enter new character to continue \n");
scanf(" %c", &x);
if(x == 'q'){
break;
}
}
return 0;
}