简体   繁体   中英

Reading in integers from a text file

For a project I am working on, I am trying to read in integers from a text file. In this case, they are formatted as variables, such as U=220V, so I am trying to read from after the equals sign to the V. This is the code I have come up with:

if (word[0] == 'U') {

                //declaring variables for checking for certain letters
                char v = 'V';
                char m = 'm';
                char M = 'M';

                //taking in integer for voltage
                while (word[i] != v)  {
                    volt = volt + word[i];
                    i++;
                }
                cout << volt << endl;

In the test case, there are three values that are U=200V,U=220V and U=22000mV (the m is milli and will be handled later with a different function) randomly inserted throughout a file. When tested, I receive the output:

200
200
20000m

The first output is correct. In the second and third cases, it seems to loose the number in the first place and in the third case it has added another zero at the end. I originally thought it was just discarding the first value, I changed the line of code

volt = volt + word[i];

to

volt = volt + word[i-1];

to see what value it would read in before the value originally first read in. In this case, it did what I would expect it to do if it were working properly, with output formatted as:

=200
=200
=20000m

Is there a flaw in the logic of my code that I am overlooking here?

EDIT: While trying to debug it further, I moved the final print line from outside the while loop to inside it to see what the loop was outputting step by step. It appears that it starts with 2 and continues to add a 0 to the end each iteration until finishing with 200000m

It seems that the issue had to with the values for i and volt not resetting after each iteration. To fix this, I added the lines of code:

volt = "";
i = 2;

(i is set to 2 in this case to overlook the original V and the = when parsing through) I then ran the program again, printing out the values to test and it appears the correct values are now outputted. So the whole block of code, updated, now looks like:

while (myfile >> word) {

        volt = "";
        i = 2;

        //printing out words (remove)
        cout << word << endl;

        //checking for U in words
            if (word[0] == 'U') {

                //declaring variables for checking for certain letters
                char v = 'V';
                char m = 'm';
                char M = 'M';

                //taking in integer for voltage
                while (word[i] != v)  {
                    volt = volt + word[i];
                    i++;
                    //cout << volt << endl;
                }
                cout << volt << endl;

What you have in your code? This string:"volt = volt + word[i];" - just take 2 count ASCII-code of number "2"(this is 50) and 1 count number "0"(this is 48). Then I take result: 50+50+48=148. I modified this code. Here it is! But there are used AnsiString and StrToInt function.

int volt=0, i=2;
char word[20] = "U=220V\0";
AnsiString String;

if(word[0] == 'U'){

    //declaring variables for checking for certain letters
    char v = 'V';
    char m = 'm';
    char M = 'M';

    while(word[i] != v){
        //volt = volt + word[i];
        String += word[i];
        i++;
        }
    //cout << volt << endl;
    volt = StrToInt(String);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM