#c++ files

1 messages · Page 1 of 1 (latest)

tawdry tartan
#

finding the average,lowest,highest number in file

buoyant vaultBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

rugged quarry
#

amazing

#

so did you remove the two ifs if so

#

can you compile now

tawdry tartan
#

yes, but the error now is "terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
: abort"

rugged quarry
#

send the code again

tawdry tartan
#
 //if option ==1
        else if (option == 1){
            string filename;
            cout << "Enter name of file: ";
            cin >> filename;
            if (filename == "COP1334C.txt"){
                ifstream myfile(filename);
                if (myfile) {
                    while (getline(myfile, line)) {
                        cout << line << '\n';

                        //calculate the average of numbers
                         count +=1;
                        average +=std::stoi(line);
                     
                    

                    }  

                }
rugged quarry
#

the entire

tawdry tartan
#
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{   
    // constants and variables
    int option = 0, average = 0, sum, count = 0;
    int lowest,highest;
    ifstream myfile;
    string courseName,profName,term,name,grade,line,infile;

    // display intro
    cout << "Course Summary App...\n" << endl;

    // do while loop for menu
    do{
         cout << "Choose one of the following options\n\t1.Process Grades Summary report\n\t2.Quit\nOption: "<< 
        endl;
        cin >> option;

        if (option <= 0 || option >= 3)
        {
            cout << "Error... Incorrect option. Try again." << endl;
        }
        
        //if option ==1
        else if (option == 1){
            string filename;
            cout << "Enter name of file: ";
            cin >> filename;
            if (filename == "COP1334C.txt"){
                ifstream myfile(filename);
                if (myfile) {
                    while (getline(myfile, line)) {
                        cout << line << '\n';

                        //calculate the average of numbers
                         count +=1;
                        average +=std::stoi(line);
                     
                    

                    }  

                }
                    


                


            }

        }


    }while (option ==1);
}
rugged quarry
#

okay are the lines printed?

tawdry tartan
#

yes, i think so

rugged quarry
#

wdym you think so you either see them or you dont

#

because that error says something has gone wrong converting the string to an int

tawdry tartan
#

i see them....

rugged quarry
#

can you show me the output?

tawdry tartan
#

like this? Choose one of the following options
1.Process Grades Summary report
2.Quit
Option:
1
Enter name of file: COP1334C.txt
COP1334C - Introduction to C++ Programming
libc++abi: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion: abort

rugged quarry
#

so COP1334C - Introduction to C++ Programming
is indeed in the file

#

obviously when converting a string to an int

#

and the string is a string

#

its a bit messy

#

how many lines are there that dont have the numbers?

tawdry tartan
#

about 22 or so

rugged quarry
#
                    int skip = 0;  
                    while (getline(myfile, line))
                    {
                        cout << line << '\n';
                        skip++;

                        
                        if (skip <= 22)
                          continue;
                        
                        // calculate the average of numbers
                        average += std::stoi(line);
                    }
#

so try this

#

this will try and skip the first 22 lines

tawdry tartan
#

the numbers are all mixed up

#

for example:

#

grade, name, grade, name

rugged quarry
#

no man we will finish this in 5hrs im sorry but for that i dont have time, please work on that yourself, id first start by calculating the avg correctly, for reading every second grade you can use the skip count and then like skip % 2 == 0 .. (odd/even)

tawdry tartan
#

i will do that, thanks for the help. i appreciate it.

buoyant vaultBOT
#

@tawdry tartan Has your question been resolved? If so, run !solved :)

digital oyster
#

Hi, applejacks

#

I am a passionate C/C++ software developer with over five years experience

#

So, I can help you perfectly.

#

please contact me.

tawdry tartan
#

okay!

buoyant vaultBOT
#

@tawdry tartan

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

tawdry tartan
#

!solved

buoyant vaultBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.