0

not very good at coding yet, and I am doing a c++ program that needs to read txt file and take line-by-line string input and iterate it through the program. each line has 18 numbers. It reads the lines but only cycles the last line. I need it to run all lines through the equation, so I can verify the checksum of each using the order set and weight with modulo 11. That part is fine, and it is working. This is the main part of the program. did not include all 200+ lines as they are just switch if else and some conversions. if needed, I can upload it all.


#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>

using namespace std;

int main()
{
    stringstream cs;
    stringstream ss;
    stringstream rr;
    stringstream mm;
    string input;
    
    //This is for manual inputs
    //cout << "Enter 18 digit number: ";
    //cin >> input;
    
    std::ifstream File ("idnumbers.txt");
    //Checking the file opening condition
    if (File.is_open())
    {
        while (std::getline (File,input))
        {  
        cout << input << '\n';
        }
            File.close();
    }
            if (input.length() != 18)
        {
            cout << "Length of ID is incorrect." << endl;
            return 1;
        }   

    int orderedSet[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    string index = "10X98765432";

    int sum = 0;
    for (int i = 0; i < 17; i++)
    {
        sum += (input[i] - '0') * orderedSet[i];
    }
Jensy
  • 13
  • 3
  • You read a line print it then read the next line throwing away the previous line you read. Then at the end you process only the last line read. You could instead process each line as you are reading. – drescherjm Jul 25 '22 at 17:57
  • You don't appear to use any of your `stringstream` variables. You may want to remove them. – drescherjm Jul 25 '22 at 17:59
  • 1
    1. Write a function that handles one single line; 2. Call that function inside a line-by-line loop. – molbdnilo Jul 25 '22 at 18:00
  • Judging by `input.length() != 18`, each line has eighteen *digits*, not numbers. – molbdnilo Jul 25 '22 at 18:01
  • 2
    If you need to process each line in the file, then you need to put the code to do the processing **inside** the loop, not after it. – john Jul 25 '22 at 18:04
  • Thank you John, I moved the closing of the while and file close to the end of program and it solved my issue. Thaks a bunch. ** drescherjm the code for those are in the other 200 plus lines of the program, thank you for taking the time to look at it. – Jensy Jul 25 '22 at 18:15
  • Your code should use functions. Don't try to do everything in `int main()` – drescherjm Jul 25 '22 at 18:23
  • Thanks, I will work on it. – Jensy Jul 25 '22 at 18:27
  • https://stackoverflow.com/questions/1567082/how-do-i-iterate-over-cin-line-by-line-in-c – CraigDavid Jul 25 '22 at 19:33

1 Answers1

0
  1. First of all you have to understand what is the use of the instruction using namespace std; Because you don't need to put std:: before the getline() for example.

  2. It's normal that your code treats only the last line of your text file because it's this line that you store last before closing the file. If you want to continue with your blocks as they are, you will have to use a container (a vector for example to store all the lines). But that would be complicating your life for nothing. To make it simpler, with your same code, you can just do this:

ifstream File ("idnumbers.txt");
    //Checking the file opening condition
    if (File)
    {
        while (getline (File,input)) //We read all lines
        {  
            cout << input << endl;
            if (input.length() != 18)
            {
                cout << "Length of ID is incorrect." << endl;
                return 1;
            }
            int orderedSet[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
            string index = "10X98765432";

            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += (input[i] - '0') * orderedSet[i];
            }
        }
        
        File.close();
    }
  1. There are variables in your code that are not used. For example, sum is not used because you don't use it.

NB: I have not tested this code and it is yours. I just moved some blocks following the logic. Soory for my poor english.

Gaël
  • 16
  • 1