Question

Am I getting this error because i declared 'n' as an int, and then asking it...

Am I getting this error because i declared 'n' as an int, and then asking it to make it a double?

This is the coude:

#include "stdafx.h"

#include <iostream>

#include <fstream>

#include <string>

#include <algorithm>

#include <vector>

using namespace std;

void sort(double grades[], int size);

char calGrade(double);

int main()

{

   int n;

   double avg, sum = 0;;

   string in_file, out_file;

   cout << "Please enter the name of the input file: ";

   cin >> in_file;

   cout << "Please enter the name of the output file: ";

   cin >> out_file;

   ofstream out;

   out.open(out_file.c_str());

   double *scores = new double[n];

   ifstream in;

   in.open(in_file.c_str());

   if (in.fail())

   {

       cout << "Unable to open file" << endl;

       exit(0);

   }

   for (int i = 0; i < n; i++)

   {

       in >> scores[i];

   }

   avg = (double)sum / n;

   cout << "A total number of " << n << " scores were processed " << endl;

   cout << "The sum of these scores is: " << avg << endl;

   cout << "The average of these scores is: " << avg << endl;

   sort(scores, n);

   cout << "The individual scores processed were: " << endl;

   char grade;

   for (int i = 0; i < n; i++){

       grade = calGrade(scores[i]);

       if (grade == 'F')

           cout << " * ";

       cout << scores[i] << " " << grade << endl;

   }

   out.close();

   cin.get();

   return 0;

}

char calGrade(double score){

   if (score >= 90 && score < 100)

       return 'A';

   else if (score >= 80 && score < 90)

       return 'B';

   else if (score >= 70 && score < 80)

       return 'C';

   else if (score >= 60 && score < 70)

       return 'D';

   else

       return 'F';

}

void sort(double grades[], int size){

   int i, j;

   double temp;

   for (i = 0; i < size; i++)

   {

       for (j = i + 1; j < size; j++)

           if (grades[i]>grades[j])

           {

               temp = grades[i];

               grades[j] = temp;

           }

   }

}

This is the error:

Error 1 error C4700: uninitialized local variable 'n' used f:\compsci235\final programm\final programm\final programm.cpp 34 1 Final Programm

Please advise on my mistake thanks.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <fstream>

#include <string>

#include <algorithm>

#include <vector>

using namespace std;

void sort(double grades[], int size);

char calGrade(double);

int main()

{

   int n = 0;

   double avg, sum = 0;;

   string in_file, out_file;

   cout << "Please enter the name of the input file: ";

   cin >> in_file;

   cout << "Please enter the name of the output file: ";

   cin >> out_file;

   ofstream out;

   out.open(out_file.c_str());

   // vector<double> scores;

   ifstream in1;

   in1.open(in_file.c_str());

   if (in1.fail())

   {

       cout << "Unable to open file" << endl;

       exit(0);

   }

    // calculate the number of entries in file

    // loop untill file ends

    while( !in1.eof() )

   {

        int temp;

        in1>>temp;

        n++;

   }

   double *scores = new double[n];

  

    // open file in read mode

    ifstream in(in_file.c_str());

    int index = 0;

    // loop untill file ends

    while( !in.eof() )

   {

        in>>scores[index];

       

        sum += scores[index];

       

        index++;

   }

   avg = (double)sum / (double)n;

   cout << "A total number of " << n << " scores were processed " << endl;

   cout << "The sum of these scores is: " << sum << endl;

   cout << "The average of these scores is: " << avg << endl;

   sort(scores, n);

   cout << "The individual scores processed were: " << endl;

   char grade;

   for (int i = 0; i < n; i++){

       grade = calGrade(scores[i]);

       if (grade == 'F')

           cout << " * ";

       cout << scores[i] << " " << grade << endl;

   }

   out.close();

   cin.get();

   return 0;

}

char calGrade(double score){

   if (score >= 90 && score < 100)

       return 'A';

   else if (score >= 80 && score < 90)

       return 'B';

   else if (score >= 70 && score < 80)

       return 'C';

   else if (score >= 60 && score < 70)

       return 'D';

   else

       return 'F';

}

void sort(double grades[], int N)

{

    int i, j;

  

    for (i = 0; i < N - 1; i++)

    {

        // the previous i elements are placed

        for (j = 0; j < N - i - 1; j++)

        {

            // swap the two elements

            if (grades[j] > grades[j+1])

            {

                int temp = grades[j];

                grades[j] = grades[j + 1];

                grades[j + 1] = temp;

            }

        }

    }

}

------------------------input.txt-------------------------

45
89
94
76
95
74
78

Sample Output

lease enter the name of the input file input.txt Please enter the name of the output file:output.txt A total number of 7 scor

Add a comment
Know the answer?
Add Answer to:
Am I getting this error because i declared 'n' as an int, and then asking it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with a C++ program. I have been getting the error "this function or variable...

    Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) {    // average function , declaring variable    int i;    char str[40];    float avg = 0;    // iterating in...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • There is a problem with thecode. I get the error messages " 'board' was not declared...

    There is a problem with thecode. I get the error messages " 'board' was not declared in this scope", \src\TicTacToe.cpp|7|error: expected unqualified-id before ')' token| \src\TicTacToe.cpp|100|error: expected declaration before '}' token| Here is the code #ifndef TICTACTOE_H #define TICTACTOE_H #include class TicTacToe { private: char board [3][3]; public: TicTacToe () {} void printBoard(); void computerTurn(char ch); bool playerTurn (char ch); char winVerify(); }; ************************************ TicTacToe.cpp #endif // TICTACTOE_H #include "TicTacToe.h" #include #include using namespace std; TicTacToe() { for(int i =...

  • I'm not getting out put what should I do I have 3 text files which is...

    I'm not getting out put what should I do I have 3 text files which is in same folder with main program. I have attached program with it too. please help me with this. ------------------------------------------------------------------------------------ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the...

  • Modify Assignment #18 so it drops each student’s lowest score when determining the students’ test score...

    Modify Assignment #18 so it drops each student’s lowest score when determining the students’ test score average and Letter grade. No scores less than 0 or greater than 100 should be accepted #include<iostream> #include<string> using namespace std; int main() { double average[5]; string name[5]; char grade[5]; for(int i=0; i<=5; i++) { cout<<"Enter student name: "; cin.ignore(); getline(cin,name[i]); int sum=0; for(int j=0; j<5; j++) { int grades; cout<<"Enter Grades:"<<j+1<<" : "; cin>>grades; sum+=grades; } average[i]=(float)sum/5; if(average[i]>=90 && average[i]<=100) grade[i]='A'; else if(average[i]>=80...

  • Today assignment , find the errors in this code and fixed them. Please I need help...

    Today assignment , find the errors in this code and fixed them. Please I need help with find the errors and how ro fixed them. #include <iostream> #include <cstring> #include <iomanip> using namespace std; const int MAX_CHAR = 100; const int MIN_A = 90; const int MIN_B = 80; const int MIN_C = 70; double getAvg(); char determineGrade(double); void printMsg(char grade); int main() { double score; char grade; char msg[MAX_CHAR]; strcpy(msg, "Excellent!"); strcpy(msg, "Good job!"); strcpy(msg, "You've passed!"); strcpy(msg, "Need...

  • C++ Code error help. I am getting the error: "expression must have a constant value, the...

    C++ Code error help. I am getting the error: "expression must have a constant value, the value of parameter 'n' ( declared at line 7) cannot be used as a constant" I am also getting this error at lines 65 and 66 with m and n. I do not know how to fix this. Here is my code and ive marked where the errors were with lines: #include<iostream> using namespace std; // Function to allocate memory to blocks as per...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

  • Three of these function overloads are considered identical by the compiler and would conflict with each...

    Three of these function overloads are considered identical by the compiler and would conflict with each other. The other would be considered different- choose the one that is considered different from others. Int foo ( int x, double y ) ; Int foo ( double x, int y ) ; Void foo ( int x, double y ) ; Int foo ( int day, double temp ) ; Consider the following incomplete code: Int f ( int number ) }...

  • C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you...

    C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you can. Level 1: (20 points) Write FUNCTIONS for each of the following: a) Validate #students, #scores. b) Compute letter grade based on average. c) Display student letter grade. d) Display course average. Level 2: (15 points) Use ARRAYS for each of the following. e) Read a student's scores into array. f) Calculate the student's average based on scores in array. g) Display the student's...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT