This is the while loop and I would like to stop the loop a character 'q' is typed as an input. So when "Please enter the five requirements" appears and I type in q then it stops running
while (1)
{
printf("Please enter the five requirements:");
scanf("%d %f %f %d %d", &my, &mii, &mr, &ml, &mp);
printf("You entered: %d %f %f %d %d\n", my, mii, mr, ml, mp);
for (i = 0; i < 15; i++)
{
if (my <= mortGage[i].min_d && mii >= mortGage[i].max_iin && mr >= mortGage[i].max_rt &&
ml <= mortGage[i].min_l && mp >= mortGage[i].max_pf)
{
printf("name is : %s\n", mortGage[i].name_b);
printf("min duration is : %d\n", mortGage[i].min_d);
printf("max initial rate is : %f\n", mortGage[i].max_iin);
printf("max rate is : %f\n", mortGage[i].max_rt);
printf("min loan is : %d\n", mortGage[i].min_l);
printf("max product fees is : %d\n", mortGage[i].max_pf);
printf("\n");
}
else
{
printf("");
}
//if (my == 'q'|| mii == 'q' || mr == 'q' || ml == 'q' || mp == 'q')
//break;
}
}
I'm not sure if it can help you. This is what I do here:
#include <stdio.h>
int main()
{
int check;
while (1)
{
printf("Please enter the five requirements:");
check=scanf("%d %f %f %d %d", &my, &mii, &mr, &ml, &mp);
if(checker==0){
printf("Input Q\n");
break;
}else{
printf("You entered: %d %f %f %d %d\n", my, mii, mr, ml, mp);
for (i = 0; i < 15; i++)
{
if (my <= mortGage[i].min_d && mii >= mortGage[i].max_iin && mr >= mortGage[i].max_rt &&
ml <= mortGage[i].min_l && mp >= mortGage[i].max_pf)
{
printf("name is : %s\n", mortGage[i].name_b);
printf("min duration is : %d\n", mortGage[i].min_d);
printf("max initial rate is : %f\n", mortGage[i].max_iin);
printf("max rate is : %f\n", mortGage[i].max_rt);
printf("min loan is : %d\n", mortGage[i].min_l);
printf("max product fees is : %d\n", mortGage[i].max_pf);
printf("\n");
}else{
printf("");
}
}
}
}
return 0;
}
Related
I want the code to execute the average marks of all students simultaneously and also determine the grade of the student after printing each average marks.
#include <stdio.h>
int main()
{
int stud_rlno[8], phy_marks[8], chem_marks[8],
bio_marks[8], CS_marks[8], average;
int i;
for(int i = 0; i < 1; ++i)
{
printf("Enter the roll number of student %d: ", i);
scanf("%d", &stud_rlno[i]);
printf("Enter the physics marks of student %d: ", i);
scanf("%d", &phy_marks[i]);
printf("Enter the chemistry marks of student %d: ", i);
scanf("%d", &chem_marks[i]);
printf("Enter the biology marks of student %d: ", i);
scanf("%d", &bio_marks[i]);
printf("Enter the CS marks of student %d: ", i);
scanf("%d", &CS_marks[I]);
}
for(int i = 0; i <= 1; i++)
{
average = phy_marks[i] + chem_marks[i] + bio_marks[i] + CS_marks[i];
printf("The average of student is %d \n", average / 4);
if(average > 90)
{
printf("Grade A \n");
}
else if(average <= 90 && average > 82)
{
printf("Grade B \n");
}
else if(average <= 82 && average > 75)
{
printf("Grade C \n");
}
else if(average <= 75 && average > 65)
{
printf("Grade D \n");
}
else if(average <= 65)
{
printf("Grade E \n");
}
else
{
printf("Fail");
}
}
}
The if statements inside the for loop don't work, Why are the If statements inside the for loop not working except for the first condition whichever it satisfies? I tried with many variations but it still doesn't work.
Check your for loop. While executing, your program will iterate for only once as you have selected condition to be for (i=0; i<1;i++). I would advise you to take what will be the size of array (no.of students) and set the value of i.
secondly,
sum = phy_marks[i] + chem_marks[i] + bio_marks[i] + CS_marks[i];
average = sum/4;
Your program calculates the average mark for a single student,(for i=0; i<1; i++). This means that it would still work without the loops. As shown below.
To use use a loop in calculating the average mark for each student, consider the second part of this answer.
int stud_rlno;
float phy_marks,chem_marks,bio_marks,CS_marks,average;
printf("Enter the roll number of student: ");
scanf("%d", &stud_rlno);
printf("Enter the physics marks of student %d: ",stud_rlno);
scanf("%f", &phy_marks);
printf("Enter the chemistry marks of student %d: ",stud_rlno);
scanf("%f", &chem_marks);
printf("Enter the biology marks of student %d: ",stud_rlno);
scanf("%f", &bio_marks);
printf("Enter the CS marks of student %d: ",stud_rlno);
scanf("%f", &CS_marks);
average=(phy_marks+chem_marks+bio_marks+CS_marks)/4;
printf("The average of student is %.2f.\n", average / 4);
if(average > 90){
printf("Grade A \n");
}
else if(average <= 90 && average > 82){
printf("Grade B \n");
}
else if(average <= 82 && average > 75){
printf("Grade C \n");
}
else if(average <= 75 && average > 65){
printf("Grade D \n");
}
else if(average <= 65){
printf("Grade E \n");
}
else{
printf("Fail");
}
To calculate the average mark for a given number of students, consider the code below:
The program prompts the user to enter the number of students. It then prompts the user to enter marks for all the four subjects, for each student. simultaneously the program prints out the marks, calculates the average and prints out the average and the grade.
#include<stdio.h>
#include<stdlib.h>
int main(){
char *subject[]={"Physics","Chemistry","Biology","CS"};
int no_of_students;
printf("Enter the number of students: ");
scanf("%d",&no_of_students);
int i,j;
float marks[100][4];
//Marks input for all subjects ,for each students.
for(i=0; i<no_of_students; i++){
printf("For student %d enter: \n",i+1);
for(j=0; j<4; j++){
printf("%s marks: ",subject[j]);
scanf("%f",&marks[i][j]);
}
}
printf("Student\tPhysics\tChemistry\tBiology\tCS\n");
float sum,average;
for(i=0; i<no_of_students; i++){
printf("%d\t",i+1);//lists the students.
sum=0;
for(j=0; j<4; j++){
printf("%.2f\t",marks[i][j]);//prints the marks.
sum+=marks[i][j];//Sums marks for individual student.
}
average=sum/4;//calculates the average per student.
printf("%.2f\t",average);//prints the average.
//Grades the average mark.
if(average > 90){
printf("Grade A \n");
}
else if(average <= 90 && average > 82){
printf("Grade B \n");
}
else if(average <= 82 && average > 75){
printf("Grade C \n");
}
else if(average <= 75 && average > 65){
printf("Grade D \n");
}
else if(average <= 65){
printf("Grade E \n");
}
else{
printf("Fail");
}
}
}
The average which is the last output should display 60 but I got it wrong. What is the error here? Here is the temperature input 72, 46, 90, 20, 70 85, 60, 40, -1000.
The total hot days should display 2 but on the output I got 3.
#include <stdio.h>
int categorize_days(int temp);
int categorize_days(int temp){
if (temp >= 85){
return 1;
}
else if(temp >=60){
return 2;
}
else{
return 3;
}
}
int main(){
int i, temp, h, p,c, temp_ave=0, type_days;
double ave;
printf("Lets check the whether !\n");
printf("Keep entering the integer temperature, enter -1000 to quit\n\n");
printf("Temperature: ");
scanf("%d", &temp);
while(temp!= -1000){
for(i = 0; i<8; i++ ){
temp_ave =+ temp;
}
type_days = categorize_days(temp);
if( type_days == 1){
printf ("Day: h\n\n");
h++;
}
else if(type_days == 2){
printf ("Day: p\n\n");
p++;
}
else{
printf ("Day: c\n\n");
c++;
}
printf("Temperature: ");
scanf("%d", &temp);
}
printf("End\n\n");
ave = temp_ave/8;
printf("Total Hot days: %d \n", h);
printf("Total Pleasant days: %d \n", p);
printf("Total Cold days: %d \n", c);
printf("Average temperature for 8 days is %f", ave);
}
The first big mistake is here:
for(i = 0; i<8; i++ ){
temp_ave =+ temp; // You are not adding temp to temp_ave, to add you should
} // write temp_ave += temp;
// Anyway this won't help you, cause you are trying to add the same number 8 times.
// So therefore it will give you wrong average, when you are calculating it.
I think this is what you wanted to do.
#include <stdio.h>
int categorize_days(int temp) {
return (temp >= 85 ? 1 : (temp >= 60 ? 2 : 3));
}
int main() {
int temp = 0, h = 0, p = 0, c = 0, cnt = -1;
double ave = 0;
printf("Lets check the whether !\n");
printf("Keep entering the integer temperature, enter -1000 to quit\n\n");
while (temp != -1000) {
ave += temp;
cnt ++;
printf("Temperature: ");
scanf("%d", &temp);
int type_days = categorize_days(temp);
if (type_days == 1) {
printf ("Day: h\n\n");
h++;
}
else if(type_days == 2) {
printf ("Day: p\n\n");
p++;
}
else {
printf ("Day: c\n\n");
c++;
}
}
printf ("End\n\n");
printf ("Total Hot days: %d \n", h);
printf ("Total Pleasant days: %d \n", p);
printf ("Total Cold days: %d \n", c);
printf ("Average temperature for 8 days is %f", ave / cnt);
}
Don't use magic numbers in the code, following and updating becomes tedious. Use #define macros as necessary.
#define HOT_CUTOFF 85 // Fahrenheit
#define COLD_CUTOFF 60 // Fahrenheit
#define INPUT_GUARD -1000
Make use of enum to list out day-types:
enum {
eHotDay = 1,
eColdDay = 2,
eCozyDay = 3
} eDayType;
Then your day_type() would change to :
int day_type (const int temp) {
if (temp >= HOT_CUTOFF)
return eHotDay;
else if (temp <= COLD_CUTOFF) // should be less than or equal to
return eColdDay;
else
return eCozyDay;
}
Always initialise variables before usage. h, p & c are being used without initialisation. Also, temp_ave is misleading name for keeping total.
int i, temp, h, p,c, temp_ave=0, type_days;
Why limit only to 8 inputs when you've -1000 as guard?
Simplified:
#include <stdio.h>
#define HOT_CUTOFF 85 // Fahrenheit
#define COLD_CUTOFF 60 // Fahrenheit
enum {
eHotDay = 1,
eColdDay = 2,
eCozyDay = 3
} eDayType;
//int categorize_days (int temp); // redundant as you're defining the function before usage
//int categorize_days (int temp) {
int day_type (const int temp) {
if (temp >= HOT_CUTOFF)
return eHotDay;
else if (temp <= COLD_CUTOFF) // should be less than or equal to
return eColdDay;
else
return eCozyDay;
}
int main() {
printf ("Keep entering the integer temperature, enter -1000 to quit\n\n");
int total = 0;
int count = 0;
int hot = 0, cold = 0, cozy = 0;
while (1) {
int temp;
if (1 != scanf ("%d", &temp)) {
printf ("ERROR: Invalid input\n");
return 1;
}
if (INPUT_GUARD == temp) break;
total += temp;
++count;
switch (day_type(temp)) {
case eHotDay : ++hot; break;
case eColdDay : ++cold; break;
case eCozyDay : ++cozy; break;
}
}
double avgTemp = 0.0;
if (count)
avgTemp = (double)total / count;
printf ("End\n\n");
printf ("Total Hot days: %d \n", hot);
printf ("Total Pleasant days: %d \n", cozy);
printf ("Total Cold days: %d \n", cold);
printf ("Average temperature for 8 days is %.2lf\n", avgTemp);
return 0;
}
The average which is the last output should display 60 but I got it wrong.
This loop is a mess. Same as temp_ave = +temp;, same as temp_ave = temp;.
for (i = 0; i<8; i++) {
temp_ave =+ temp; // ????????
}
Instead:
temp_ave += temp; // Note +=
temp_ave/8 in an int division. Instead, perform the division per the type of ave avoiding casts.
// ave = temp_ave/8;
ave = temp_ave;
ave /= 8; // Division done per wider of type of `ave` and `int` constant `8`.
The total hot days should display 2 but on the output I got 3.
Enable all warnings.
Initialize h, p, c. #Weather Vane
After confirming the number is positive or negative, I want to go back to the first initial printf.
what should i do?
#include <stdio.h>
#include <conio.h>
main()
{
int a, b, c, max;
printf("Entry bill 1 : "); scanf("%i", &a);
printf("Entry bill 2 : "); scanf("%i", &b);
printf("Entry bill 3 : "); scanf("%i", &c);
if ((a > b) && (a > c))
max = a;
if ((b > a) && (b > c))
max = b;
if ((c > a) && (c > b))
max = c;
printf("Bil terbesar adalah : %i \n", max);
if (max > 0)
printf("Bil tersebut adalah positif \n");
if (max < 0)
printf("Bil tersebut adalah negatif \n");
return 0;
}
maaybe you can use do-while loop and add some condition looping, like this
main()
{
boolean isloop = true;
do{
int a,b,c,max;
printf("Entry bill 1 : "); scanf("%i",&a);
printf("Entry bill 2 : "); scanf("%i",&b);
printf("Entry bill 3 : "); scanf("%i",&c);
if((a>b)&&(a>c))
max=a;
if((b>a)&&(b>c))
max=b;
if((c>a)&&(c>b))
max=c;
printf("Bil terbesar adalah : %i \n",max);
if(max>0)
printf("Bil tersebut adalah positif \n");
if(max<0)
printf("Bil tersebut adalah negatif \n");
// you can setting value variable isLoop = false, to stop looping
}while(isLoop);
return 0;
}
Basically this is a program to play with dice but i have a problem with srand. it gives an error stating "implicit conversion loses integer precision: 'time_t' (aka 'long') to 'unsigned int'"
whys that ?
Also i would like to know if there are any other ways to improve this code? currently i only know stdio.h i know there are some out there like iostream or something like that. is there a possible way to explain those to me also.
Here is the code.
i know crt secure no warning is to ignore some errors but i would still like to know why it the error pops up ?
#define _CRT_SECURE_NO_WARNINGS
#define MAX_CHIPS 400
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void display_details(void);
int throws_die(void);
int main() so on
{
int die1, die2, throw_sum;
int PointToMake;
int Games_Played = 0;
int Games_Won = 0;
int Games_Lost = 0;
int CurrentChips = MAX_CHIPS;
int BetChips;
char PlayerName[30];
char PlayAgain;
display_details();
srand(time(NULL));
printf("Would you like to play Test your luck [y|n]?\n");
scanf(" %c", &PlayAgain);
while ((PlayAgain == 'y') && (CurrentChips > 0))
{
if (Games_Played == 0)//shows how many games played which is obviously 0
{
printf("A new chellenger! What is your name?\n");
scanf(" %s", &PlayerName);
printf("Hi %s!\n", PlayerName);
}
Games_Played = Games_Played + 1;//each new game it will increase by 1
printf("Number of chips: %d\n", CurrentChips);
printf("Place your bet:\n ");
scanf("%d", &BetChips);
while ((BetChips < 0) || (BetChips > CurrentChips))
{
printf("uuh ....You can only bet the chips you have.. (0-%d)!\n", CurrentChips);
printf("Place your bet: \n");
scanf("%d", &BetChips);
}
die1 = throws_die();
die2 = throws_die();
throw_sum = die1 + die2;
printf("You Got: %d + %d = %d\n", die1, die2, throw_sum);
if ((throw_sum == 7) || (throw_sum == 12))
{
//+1 to games won
Games_Won = Games_Won + 1;
//adding to bet balance
CurrentChips = CurrentChips + BetChips;
printf("XXXXX Winner! d(^_^) XXXXX\n");
}
else if ((throw_sum == 2) || (throw_sum == 3) || (throw_sum == 10))
{
Games_Lost = Games_Lost + 1;
CurrentChips = CurrentChips - BetChips;
printf("XXXXX Loser! :( XXXXX\n");
}
else
{
PointToMake = throw_sum;
printf("Points to make is: %d\n", PointToMake);
die1 = throws_die();
die2 = throws_die();
throw_sum = die1 + die2;
printf(" |--->> Spinned: %d + %d = %d\n", die1, die2, throw_sum);
while (throw_sum != PointToMake && throw_sum != 7)
{
die1 = throws_die();
die2 = throws_die();
throw_sum = die1 + die2;
printf(" |--->> Spinned: %d + %d = %d\n", die1, die2, throw_sum);
}
if (throw_sum == PointToMake)
{
printf("XXXXX Winner! (x2 the bet) XXXXX\n");
//x2 added to balance
CurrentChips = CurrentChips + 2 * BetChips;
Games_Won = Games_Won + 1;
}
else
{
Games_Lost = Games_Lost + 1;
CurrentChips = CurrentChips - BetChips;
printf("XXXXX Loser!:( XXXXX\n");
}
}
if (CurrentChips > 0)
{
printf("You now have %d chips.\n", CurrentChips);
printf("============================\n\n");
printf("Play Again %s [y|n]? ", PlayerName);
scanf(" %c", &PlayAgain);
printf("============================\n");
}
else
{
printf("you're out of chips.\n");
printf("XXXXX GG TRY AGAIN NEXT TIME XXXXX\n");
}
}
if (Games_Played == 0)
{
printf("Oh well.... better luck next time!\n");
}
else if (Games_Played == 1)
{
printf("%s played %d game and is cashing out with %d chips!\n", PlayerName, Games_Played, CurrentChips);
printf(" |---> Games won: %d\n", Games_Won);
printf(" |---> Games lost: %d\n", Games_Lost);
printf("Thanks for playing, come again to test that luck of yours %s.\n", PlayerName);
}
else
{
printf("%s played %d games and is cashing out with %d chips!\n", PlayerName, Games_Played, CurrentChips);
printf(" |---> Games won: %d\n", Games_Won);
printf(" |---> Games lost: %d\n", Games_Lost);
printf("\nThanks for playing, come again to test that luck of yours %s!\n", PlayerName);
}
return 0;
}
void display_details(void)
{
printf("File :.... assignment.c\n");
printf("Author :.... \n");
printf("Stud ID :.... \n");
printf("Email ID :.... \n");
printf("This is my own work as defined by the \n");
printf("University's Academic Misconduct Policy.\n");
}
int throws_die(void)
{
return 1 + rand() % 6;
srand is defined more or less like this:
void srand(unsigned int seed);
time is defined more or less like this:
__int64 time(__int64 *t);
Thus you pass an __int64 (64 bits) into an unsigned int (32 bits) parameter, which is not possible without potential trunctation. That's what the warning message is telling you.
I'm learning C, and I'm trying to make a basic calculator, but having some trouble with the while statements. I've tried doing it multiple ways but it never repeats itself, just finishes the script.
Any ideas?
//
// main.c
// Calculator
//
// Created by Austen Patterson on 2013-06-27.
// Copyright (c) 2013 Austen Patterson. All rights reserved.
//
#include <stdio.h>
#include <stdbool.h>
int main()
{
int number[100];
int operator = '0';
int doAgainAnswer = '0';
bool doAgain;
do{
printf("Please enter your first number:");
scanf("%d", &number[1]);
printf("\nYou entered %d as your first number. Please enter your second: ", number[1]);
scanf("%d", &number[2]);
printf("\nYou entered %d as your second number.", number[2]);
printf("\nYour numbers are now %d and %d", number[1], number[2]);
printf("\nNow enter your operator.\n1 for addition\n2 for subraction\n3 for multiplication\n4 for division.\n");
scanf("%d", &operator);
if(operator == 1){
int finished = number[1] + number[2];
printf("\n\n%d \+ %d is: %d", number[1], number[2], finished);
}
if(operator == 2){
int finished = number[1] - number[2];
printf("\n\n%d \- %d is: %d", number[1], number[2], finished);
}
if(operator == 3){
int finished = number[1] * number[2];
printf("\n\n%d \* %d is: %d", number[1], number[2], finished);
}
if(operator == 4){
int finished = number[1] / number[2];
printf("\n\n%d \/ %d is: %d", number[1], number[2], finished);
}
printf("\nWant to continue?\n 1 for Yes\n 2 for No\nAnswer: ");
scanf("%d", &doAgainAnswer);
if(doAgainAnswer == 1) {
doAgain = '1';
} else {
doAgain = '0';
}
}while(doAgain == '1');
}
Edited code
#include <stdio.h>
#include <stdbool.h>
int main()
{
int number[100];
int operator = '0';
int doAgainAnswer = 0;//edited
int doAgain=0;//edited
do{
printf("Please enter your first number:");
scanf("%d", &number[1]);
printf("\nYou entered %d as your first number. Please enter your second: ", number[1]);
scanf("%d", &number[2]);
printf("\nYou entered %d as your second number.", number[2]);
printf("\nYour numbers are now %d and %d", number[1], number[2]);
printf("\nNow enter your operator.\n1 for addition\n2 for subraction\n3 for multiplication\n4 for division.\n");
scanf("%d", &operator);
if(operator == 1){
int finished = number[1] + number[2];
printf("\n\n%d \+ %d is: %d", number[1], number[2], finished);
}
if(operator == 2){
int finished = number[1] - number[2];
printf("\n\n%d \- %d is: %d", number[1], number[2], finished);
}
if(operator == 3){
int finished = number[1] * number[2];
printf("\n\n%d \* %d is: %d", number[1], number[2], finished);
}
if(operator == 4){
int finished = number[1] / number[2];
printf("\n\n%d \/ %d is: %d", number[1], number[2], finished);
}
printf("\nWant to continue?\n 1 for Yes\n 2 for No\nAnswer: ");
scanf("%d", &doAgainAnswer);
if(doAgainAnswer == 1) {
doAgain = 1;//edited
} else {
doAgain = 0;//edited
}
}while(doAgain == 1);//edited
return 0;//edited
}
Change the while Condition to
while(doAgain==1)
And in all the doAgain assingments, use 0 or 1.
Other way would be to use While(1) and break when user enters 0 at prompt in doAgainAnswer.