Thread: Converte from c++ to c

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    3

    Converte from c++ to c

    Can anyone help me to convert this code from c++ to c
    Code:
    #include <iostream>
    
    #include <iomanip>
    
     
    
    using namespace std;
    
    float calcAvgGrade(float G1, float G2, float G3, float G4)
    
    {
    
    return (G1 + G2 + G3 + G4)/4;
    
    }
    
     
    
    char findLetter(float Grade)
    
    {
    
    if(Grade >= 90)
    
    return 'A';
    
    else if(Grade >= 80)
    
    return 'B';
    
    else if(Grade >= 70)
    
    return 'C';
    
    else if(Grade >= 60)
    
    return 'D';
    
    else if(Grade >= 50)
    
    return 'F';
    
    else return 'U';
    
    }
    
    int main()
    
    {
    
    freopen("input_File.txt", "r", stdin);
    
    freopen("output_File.txt", "w", stdout);
    
    string name;
    
    while(cin >> name)
    
    {
    
    string lastName;
    
    cin >> lastName;
    
    float grades[4];
    
    for (int i = 0; i < 4; ++i) {
    
    cin >> grades[i];
    
    }
    
    float avg = calcAvgGrade(grades[0], grades[1], grades[2], grades[3]);
    
    char grade = findLetter(avg);
    
     
    
    cout << name << " " << lastName << '\t' << "Average: " << fixed << setprecision(2) << avg << '\t' << "Grade: " << grade << endl;
    
    }
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Can anyone help me to convert this code from c++ to c
    It would be better if you just started from scratch, find a good C solution instead of the poor C++ solution you have currently found. But whatever the case you need to show what you've tried.

  3. #3
    Registered User
    Join Date
    Jan 2019
    Posts
    3
    Ok my friend I have tried with this code :
    Code:
    #include <stdio.h>
    
    
    
    float calculate_average(float grade1,float grade2,float grade3,float grade4)
    
    {
    
    float avg;
    
    avg = (grade1+grade2+grade3+grade4)/4;
    
    return avg;
    
    }
    
    
    char find_letter_grade(float avg)
    
    {
    
    
    char lettergrade;
    
    if(avg>=90 && avg<=100){
    
    lettergrade='A';}
    
    else if(avg >=80 && avg<=89.9){
    
    lettergrade='B';}
    
    else if(avg >=70 && avg<=79.9){
    
    lettergrade='C';}
    
    else if(avg >=60 && avg<=69.9){
    
    lettergrade='D';}
    
    else if(avg >=50 && avg<=59.9){
    
    lettergrade='F';}
    
    else if(avg >=0 && avg<=49.9){
    
    lettergrade='U';}
    
    }
    
    
    int main()
    
    {
    
    
    FILE *inputfile;
    
    FILE *outputfile;
    
    char buffer[500];
    
    float grade1,grade2,grade3,grade4, avg;
    
    char firstname[50];
    
    char lastname[50];
    
    char grade[50];
    
    
    
    inputfile=fopen("input_file.txt","r");
    
    if (inputfile == NULL)
    
    {
    
    printf("File does not exists \n");
    
    return 0;
    
    }
    
    
    
    while (feof (inputfile) ==0)
    
    {
    
    fscanf(inputfile,"%s,%s,%f,%f,%f,%f",firstname,lastname,&grade1,&grade2,&grade3,&grade4);
    
    calculate_average(float grade1,float grade2,float grade3,float grade4);
    
    find_letter_grade(float avg);
    
    printf(outputfile,"%f\t,%s\t,%s\t,%s\t",avg,lettergrade,firstname,lastname);
    
    fgets(buffer,500,inputfile);
    
    }
    
    
    return 0;
    
    
    }
    But this code doesn't work.. Can you help me to correct the code?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But this code doesn't work..
    Why doesn't it work?

    What does your input file look like?

    You really need to find and use a decent indent style, your current style (really no style) makes your code hard to read and makes following the program logic very difficult.

    Edit: By the way you should never use a method to retrieve a C-string that doesn't limit the number of characters it will try to retrieve. The scanf() series of functions can be limited by using the correct width specifier.
    Last edited by jimblumberg; 01-18-2019 at 09:26 AM.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    And what happens if the avg is, e.g., 89.95 ?
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Tags for this Thread