I was wondering if I implemented my vector elements correctly? I am trying to create a CRC program for homework. If you have suggestions on how to clean up the code (I am sure it is in serious need of it, i just don't know how to do it) Please let me know, and help me understand how to do it!
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#define SignatureBlock "~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\nNicholas Whitmore\nnicholas.whitmore@maine.edu\nCIS 215, C++ Application Programming\n~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\n"
usingnamespace std;
using std::string;
int main()
{
cout << SignatureBlock << endl;//implement signature block
cout << "" << endl;
cout << "CRC Check Program\n\n" << endl;//title
cout << "Would you like to check a file or a string?\n" << endl; //prompt for user input
cout << "1) To Check a File\n" << endl;//option 1
cout << "2) To Check a String\n" << endl;//option 2
cout << "3) To exit\n" << endl;//option 3
int select; //user selection variable
cin >> select;//user input
if (select == 1)//user chose option 1
{
cout << "Enter the name of the file including path:" << endl;//prompt for file path
string filePath; //file path variable
cin.ignore();
getline(cin, filePath); //user input
cout << "File mode:\n"<<endl;//file mode title
cout<<"Read 31 bytes from " << filePath<<endl;
cout<<"Check byte is: " <<endl;
ifstream myfile(filePath.c_str());//opens file at filePath
string line;
if (myfile.is_open())//check file path
{
while (getline(myfile,line))
{
cout<<"Data is: "<< line << endl;//output the data within the file
}
myfile.close();//close the file
}
else cout << "Unable to open file" << endl;//if file path is incorrect
vector<bool> current_bits; // vector used so the size can be changed
int i,j;
char current_byte; // byte pointer
for(i=0; i<line.length();++i) // iterate through the entire string
{
current_byte = line.at(i); // get the character at the current location
for(j=7;j>=0;--j) // iterate through all 8 bits
{
current_bits.push_back((current_byte & (1<<j)) != 0);
}
}
}
elseif (select == 2)//user chose option 2
{
cout << "Enter the string to be checksummed:" << endl;//prompt for string
string cString; //string to be checked variable
cin.ignore();
getline(cin, cString);//user input
cout<<"Text mode:\n"<<endl;
vector<bool> current_bits; // vector used so the size can be changed
int i,j;
char current_byte; // byte pointer
for(i=0; i<cString.length();++i) // iterate through the entire string
{
current_byte = cString.at(i); // get the character at the current location
for(j=7;j>=0;--j) // iterate through all 8 bits
{
current_bits.push_back((current_byte & (1<<j)) != 0);
}
}
cout<<"CRC of [" << cString << "] is [] with P= []\nHexadecimal checksum: "<< endl;
}
elseif (select == 3)//user chose option 3
{
cout << "Thank you! Have a nice day!" << endl; //exits the program
}
else//user chose an invalid option
{
cout << "You have chosen an invalid option. Good bye." << endl;//invalid option. program closes
}
cin.get(); //keeps the command prompt window open
return 0;
}//end main()
NOTE: the code is not complete yet. If you know how and where I would implement the CRC algorithm, that would also be very helpful ;)
I was thinking i would write the algorithm as a void() function, and let the program end there. Is that a good plan?