How do I make a char vector recognize a line break?

Asked

Viewed 61 times

0

The code is like this

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

int main(){

    char frase[30];
    
    char j;
    
    int i=0;
    do{
        cin>>j;
        if(j!=' '){
            frase[i]=j;
            i++;
        }
    }while (j!=' ');
    
    
    
    return 0;
}   
  • a line break is an escape character equal to \n, have tried to verify comparing to this?

  • already, does not work

1 answer

1

The line break is a character of "escape", are characters of type "ENTER" or "TAB". In the case of line break is the " n", and can be compared like this == "\n", but some characters are not possible because it does not have "escape" characters, such as the case of a "DEL" for example, so one way to cover all characters is to convert the one char for int that will return the code ASCII, which will be 10 and compare, so:

int ascii;
....
ascii = (int)frase[i];

if (ascii == 10) // é uma quebra de linha

You can test with this little program:

#include <stdio.h>

int main() {

     int ascii, i;
     char texto[] = "Bom dia\nGood Moring\nBonjour";
     
     printf("%s\n------------\n", texto);
     
     for(i=0; texto[i]; i++) {
        ascii = (int)texto[i];

        if (ascii == 10) {
            printf("Quebra de linha\n");
        } else {
            printf("%c = %d\n",texto[i],ascii);
        }
    }
}

Or see online working here: https://www.mycompiler.io/view/0utzo5p

And you can see the table with the other codes ASCII here: https://sites.google.com/a/aebenfica.org/apontamentos-tic/tic/ascii

OBS: I edited the question based on the comments to better reflect the comparison

  • thank you so much..

  • if the question was helpful do not forget to vote and accept the answer if solved the problem :)

  • 1

    The line break can be compared directly like any other char, the conversion to int is not necessary. Here your code comparing directly texto[i] with \n instead of ascii == 10.

  • 1

    yes @Júlioeveneto but I put an example that can cover any character with the ASCII table being wider, and if it was a no escape character like the "omega" syllable or a "DEL" character for example, as you would compare?

  • 1

    @Ricardopunctual My remark is about your assertion that the "line break" character cannot be directly compared as char. In fact there are symbols that we can not do this, but this is not the case of line breaking. Anyone who reads your answer may think it is not possible to do so '\n' == texto[i] to find a line break and so I made the observation.

  • 1

    ok @Júlioevêncio understood your point, I edited the question by putting this information, see pf. But also think about your statement "The line break can be compared directly like any other char", this is also not correct, characters that cannot be typed and have no escape cannot be compared in the same way as "any other char" :)

  • @I think your answer is better now. Well, I studied C/C++ a little over 1 year ago and I always thought that line breaking was a character of the type char any and the \n was its symbol ASCII. Now I was in doubt, there is difference between 'A' != 'b' and '\n' != 'b'?

  • 1

    yes, the characters called "escape" are representations of characters that are not so to say "available" directly on the keyboard, and are preceded by backslash, for example \n 2 characters, they count as 1 and represent the char code ASCII 10, as well as the \t represents the TAB, ASCII code 9 and so on. In your example both will return true because 'A' is different from b (ASCII codes 65 and 98 respectively) and ' n is different from b (codes 10 and 98). "Escape character" table: https://en.cppreference.com/w/cpp/language/escape

Show 3 more comments

Browser other questions tagged

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