Hows my code? C++

So I've been working on developing a way to parse a complex input file. So far I've got all the values read in as strings and stored in a dynamically sized vector. Take a look:

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
...
    int lineCount = 0;
    string buffer = "";
    std::vector<string> fileDump;
    ifstream in(argv[1]);
    if(!in.good()){
       cout << "Can't open file, or no file has been passed via arguments!" << endl;
    } else {
           cout << "File opened, begin reading...";
           do {
               getline( in, buffer );
               fileDump.push_back(buffer);
               lineCount++;
           }
           while(in.peek() != EOF);
           in.close();
           cout << "...complete!" << endl << lineCount << " line(s) read, and " << int(fileDump.size()) << " values stored." << endl;
    }
    //lambda expression to step thru the vector
    for_each( fileDump.begin(),
              fileDump.end(),
              [] (string s){
                //cout << s << endl;
                //work in progress
              }
    );

And here is my header file:

#ifndef CLASSES_H
#define CLASSES_H
#include <iostream>
#include <fstream>
#include <string>

class Assignment
{
   private:
    std::string assignmentType;
    int currentNumberOfGrades;
    int totalNumberOfGrades;
    double percentOfFinalGrade;
    double* allGrades;
   
   public:
    Assignment();
    Assignment( std::string, int, int, double );
    std::string getAssignmentType();
    int getCurrentNumberOfGrades();
    int getTotalNumberOfGrades();
    double* getAllGradesArray();
    double getPercentOfFinalGrade();
    void setAssignmentType( std::string );
    void setCurrentNumberOfGrades( int );
    void setAllGradesArray( int );
    void setTotalNumberOfGrades( int );
    void setPercentOfFinalGrade( double );
    void updateGradesArray();
};
#endif

And here is what an input file looks like:

3        <--- Types
Lab      <--- Type of description
4        <--- Current Grade Count(bellow are the grades)
100      <--- Grade 1, and so on for the next...
90
70
88
10       <--- Total
10.0     <--- Weighted percentage
         <--- Separator
Quiz
3
89
75
92
5
15.0

Exam
1
89
2
30.0

*Only a sample, the professor will run with a file that follows the same format but will vary in length and have different data.

I've tested it and it runs just fine for storing all the values I need. However, the values are stored all as strings. I am not all that well versed in vectors and have been reading the C++ reference about them. What I would like to know if there was an easy way to parse out my ints / store them as ints. Think I need to run some code to check them as I read them in, or do vectors have built in functions for parsing?

1) A vector is template class. It's a container that can store any type you set it to...

vector<int> myIntArray;
vector<string> myStringArray;
// etc

2) There are many ways to convert strings to int...

sscanf(input, "%d", &number); // old... not used anymore
cin >> dec >> myInt;  // cpp new way with streams
myInt = atoi(c-string); // old... not used anymore
myInt = stoi(string);  // uses string class.

3) I'd have to look it up but I'm pretty sure there's a command to read in the whole file so you don't need that loop. Not sure atm though.

1 Like

Not important, but I think in you 2nd block of example you mean sscanf ^^"

derp.... thx

thanks for getting back to me on this

Strange, I got a very similar HW, what college do you go to?

It's actually a very common college program prompt.

for my class we have been building a terminal game in C and so we are going into C++ so we had to interface C and C++. My next HW I have to write code to read txt files to setup a game.

fun stuff...

It's a real shame they don't teach C++ before C. There's so much C++ has to offer but to really wrap your head around it you need to not think about C.

If you're gonna use function pointers to do that interface... check out std::function instead. It's amazing. It lets you get pointers to member functions.

http://www.cplusplus.com/reference/functional/

Well we are converting the whole project over to C++ now but I simply used void pointers to hold the classes in C and then had a function that used extern "C" {} to declare functions for C and C++ and pass the objects as a parameter and then cast it back to an object.

Since they are using functions as objects is it possible to do functional programming with that?

Also I have found I like C and C++ much better than Java and I kind of like assembly so I was wondering how one would get started in embedded systems since your description says you do that?

That's cause they are the mother of languages.

What's your current degree in?

BS in computer science, I know you might say that I should switch to EE or COMPE for embedded stuff but I also want to go into studies on artificial intelligence, at the moment I just want to start some side project involving embedded systems to play around with it.

oh... well if you want to do AI then you're in the right place.

Embedded is a totally different domain space. And yes... you'd need compE.

If you want to just learn on the side...

  • Learn hardware abstraction.
    • start with micro controllers. (PICs, megas, anything except arduino really)
    • learn to read datasheets on control registers
    • familiarize yourself with their peripherals (i2c, spi, pwm, gpio, adc, dac... etc) and use them for things.
  • Learn efficient coding methods in C
    • balance of memory / processor resources
    • what not to do on tiny little processors
    • how these RISK/i386/x86/other architectures work
  • Get involved in building robotics. Does your uni have a chapter of IEEE? What are they up to?

I used to go to IEEE here but all that would happen is either they would talk about how great they were or a company would give their speech. I just go to our local computer science club now. I also had a bit of a falling out with one of their cabinet members because he was kind of a prick in a short story lol. I am better at self teaching anyway.

I am also in a computer architecture class where we have had to write the states of a finite state machine with a given architecture to do some type of operation that would control the data path of a simple architecture. With that we have learned some assembly as well. On top of that I had to take a course on digital design or digital logic where I had to design a random number generator. Finally as you know I am in a C and C++ course. I think these are relevant to embedded systems.
https://github.com/sdrafahl/Hardware-Random-Number-Generator

If I wanted to buy something cheap to do a project where would you recommend I look?

https://getchip.com/pages/chip
https://getchip.com/pages/pocketchip

https://www.sparkfun.com/categories/2
https://www.adafruit.com/

1 Like

How's your code in terms of what? If you just want something that works then that's fine. If you want something that's highly optimized... no it's not good.

How would you optimise it?

No worries, it's C. and like you said, you shouldn't use this kind of old stuff in writing new C++ code. ;-)