Vectors correct?

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#define SignatureBlock "~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\nNicholas Whitmore\nnicholas.whitmore@maine.edu\nCIS 215, C++ Application Programming\n~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\n"
using namespace 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); 	
	                 }
                   }
            }
        else if (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;
            }
        else if (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?
Last edited on
Topic archived. No new replies allowed.