Supporting code snippets for OOP take home assignment
Open a file and read data line by line.
You're given a text(.txt) file as the input and not allowed to do manual user inputs. So you have to read the text file using your program. It can be done in the following way.Here the line is taken into a String called 'line'. To read lines you have to use the library 'fstream'
#include <fstream>
string line;
ifstream myfile ("yourfilename.txt");
if (myfile.is_open()){
while(getline(myfile,line)){
// Enter your code here
}
myfile.close();
}
src : http://www.cplusplus.com/doc/tutorial/files/Spliting string into two with blank space
Method 1
Here 'line' is the String which we read from the file. To store the two sub strings we use a vector called tokens of type String.If you use this method after splitting the string into two parts you have to cast it into int.
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
string line;
istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> tokens(beg, end);
cout << "1st sub String : " << tokens[0];
cout << "2nd sub String : " << tokens[1];
src : http://www.cplusplus.com/forum/beginner/87238/Method 2 - Spliting and casting at once
In this method you can do both spliting the string and casting at once.
The whole line is taken into the string 'line'. Then the substrings are taken into the variables 'subcode' and 'count'. Here subcode is a string while count is an int, when assigning values to count it will automatically cast the string to an int and store.
#include <string>
#include <sstream>
#include <iterator>
string line;
string subcode;
int count;
stringstream ss(line);
ss >> subcode >> count;
cout << subcode << " " << count << endl;
Casting string to an int
After splitting the string into two parts, and we consider the first line,- tokens[0] is the subject code.
- tokens[1] is the count (number of students for that subject)
We need the count to be an integer for calculations, followig snippet can be used to cast the string to an int. Here n is an integer variable and the count is stored into it.
#include <iostream>
#include <sstream>
int n;
stringstream num(tokens[1]);
num >> n;
src : https://www.geeksforgeeks.org/converting-strings-numbers-cc/Writing to the Summary(.txt) file
Writing to a file is similar as using 'cout' to show an output.
#include <fstream>
ofstream myfile;
myfile.open ("outputfile.txt");
myfile << Write your output text here << fixed;
src : http://www.cplusplus.com/doc/tutorial/files/Calculate Standard Deviation
You have to use the 'cmath' library#include <cmath>
int array[10];
float average;
float std_dev=0.0;
for(int i=0; i<10; i++){
std_dev += pow(array[i] - average, 2);
}
float sd = sqrt(std_dev / count);
cout << "Standard Deviation : " << sd << endl;
Get the length of a String
You can use the length() function to find the length of a string. You have to include the string library
#include <string>
string word = "Hello";
int size = word.length();
cout << size; // It will print 5
Retain on the screen until a key is pressed
You can use,
#include <conio.h>
getch();
or
#include <iostream>
system("pause");
Clear screen
#include <cstdlib>
if(system("CLS")) system("clear");
You can use the following C++ library insted of all the above mentioned libraries
#include<bits/stdc++.h>
<3 <3 thank you machn
ReplyDeleteYou're welcome machan 😋
Delete