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...

  • 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 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...

  • I am getting an error that the variable num was not declared in the scope on...

    I am getting an error that the variable num was not declared in the scope on the line of code that has; while(num != 5). Do you know how to fixe this error? #include <iostream> #include <iomanip> #include <string> #include<fstream> #include <cstring> using namespace std; //Declare the structure struct Games { string visit_team; int home_score; int visit_score; }; // Function prototypes int readData(Games *&stats); int menu(); void pickGame(Games *&stats, int size); void inputValid (Games *&stats, int size); void homeTotal(Games *&stats,...

  • 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...

  • 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...

  • I am receiving an "undefined reference to" build error in my code that i do not...

    I am receiving an "undefined reference to" build error in my code that i do not know how to fix. Here is my main code in C++ (the line of code that is in question is in bold): #include <iostream> #include<iomanip> #include<stdio.h> #include<stdlib.h> using namespace std; //Prototypes of functions void sortTestScores(int*testScores,int size); double averageTestScore(int*testScores,int size); void printTestScores(int*testScores, int size); int main() { //variable declaration int *testScores; int size; int score; double average; //Prompt to enter size of array cout<<"Enter size...

  • (C++) I need to alter the code below to fit the given requirements. You will need...

    (C++) I need to alter the code below to fit the given requirements. You will need to move the calculations to a function. A second function to return the Letter Grade. You will need to define a namespace to contain the CONST. Also, need to define an enum for letter grades: A, B, C, D, and F. Console Student Grade Calculation Form First Name: Larry Last Name: Hobbs Homework Average: 65 Midterm Grade: 90 Final Exam Grade: 75 Student: Larry...

  • 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++ Programming I have finished the code but there's one error which shows that "strcpy" might...

    C++ Programming I have finished the code but there's one error which shows that "strcpy" might be unsafe. Consider using strcpy_s instead. I've tried that but it's not working probably because I didn't implement it correctly. I am posting my code below. It'd be really helpful if you could fix all the strcpy errors and post it below. Thank you. Text.cpp: #include <iostream> #include <iomanip> #include <cassert> #include <cstring> #include "Text.h" Text::Text ( const char *charSeq ) {    bufferSize...

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