How to remove the string from a given range

Asked

Viewed 97 times

-1

Hello,

Guys, I’m recalling programming (c++) and I took a little project to make one of a program that reads barcodes, records in a vector. Within each code read between characters 24 and 33, has a number that references the Invoice. I need to take out that number and display it along with the code.

Go on, what I’ve already done:

#include<iostream>
#include<string.h>
using namespace std;

string cod[100];
char numnota;


int main()
{  
    string cod[100];
    cout<<"Comece aqui a ler os codigos de barra" << endl;
    int i=0;
     while(cod[i] != "0"){
         cin>>cod[i+1];
         i++;
    }

    i=0;
    while(cod[i]!="0")
    {
        cout << cod[i] << endl;
        i++;
    }
    numnota = cod.substr(24,33);

}
  • Here: while(cod[i] != "0"){ should not be: while(cod[i] != \0'){? If you want a substring then numnota should not be declared as a single character, but rather as a string. Since you are not working with the string class maybe you should use: strncpy(numnota, cod+23, 10); (remembering to correct the statement of a note).

  • Speak, Daniel Thank you for answering. Then the while and the stop condition, when someone type 0, indicates that finished reading the bar codes.I tried to use the function I said, but it didn’t work very well =/

  • I’m not Daniel, but what you mean to say is that in your barcode, zero is not allowed to exist? As for the function you added the terminator ' 0' at the end?

  • As you say you are using C++ then it is easier to use the function substr class <string>.

  • In fact, "0" is the stop condition of while, the bar codes will be read normally and when the user type 0, the code closes the reading. I’ll try to use the class you told me

1 answer

0

See if this is it (I changed the substring limits to have a smaller input):

#include <iostream>
#include <string>
using namespace std;

string cod[100];
char numnota;


int main() {
    string cod[100], numnota;
    cout<<"Comece aqui a ler os codigos de barra" << endl;
    int i=0;
    do {
        cin>>cod[i++];
    } while(cod[i-1] != "0");
    i=0;
    cout << "Códigos informados:" << endl;
    while(cod[i]!="0") {
        cout << cod[i];
        numnota = cod[i].substr(4, 3);
        cout << "\t" << numnota << endl;
        i++;
    }
    return 0;
}

See working in: https://ideone.com/a5NoWG

  • Fantastic, I really appreciate your answer. But could you explain to me how the code works? Like I said I’m relearning and I’d like to know how it works

Browser other questions tagged

You are not signed in. Login or sign up in order to post.