Use a space or "" as an element of a string in c ++

3

I want to have a space as an element of a string in c ++

this is my code:

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

int main(){
int n;
cin>>n;
cin.ignore();
int cont=1;
while(n--){
    cout<<"Message #"<<cont<<endl;;
    string mensaje[2000];
    for (int i = 0; i < 2000; ++i){
        mensaje[i]="0";

    }
    for (int i = 0; i < 2000; i++){

        cin>>mensaje[i];
        if(mensaje[i]!="0"){
            cout<<"["<<mensaje[i]<<"]";
        }

    }

    cont++;
    cout<<endl;
}
return 0;
}

and when I do the test with a text file to put from the terminal in Linux let's say ./example < example.txt

This is the text file:

2
... --- ...
.--- --- -...  -.. --- -. .  ..--..  ..-. .. -. . -.-.--

print this to me:

Message #1 [...][---][...][.---][---][-...][-..][---][-.][.][..--..][..-.][..][-.][.][-.-.--] Message #2

and I need it:

Message #1 [...][ ][---][ ][...] Message #2 [.---][ ][---][ ][-...][ ][ ][-..][ ][---][ ][-.][ ][.][ ][ ][..--..][ ][..-.][ ][..][ ][-.][ ][.][ ][-.-.--]

Thanks for your cooperation.

Sorry for not having specified it before, I am solving the 11223 grape exercise and therefore I require the 2000 strings, and I need to know when there are spaces or not because I need to make a kind of Morse code translator, and the space is to identify if it is another letter and if they are two spaces in a row they are a space in the morse already translated.

    
asked by Nicolás Ibagón 31.03.2018 в 06:40
source

1 answer

6

Your code does not make sense, surely you have confused some C ++ concepts.

The line:

string mensaje[2000];

Create two thousand objects of type std::string initialized to the default value (ie: empty); I suppose you were thinking that you were creating a single text object capable of holding two thousand characters ... but I can not know your intentions with certainty, I can be wrong.

This loop:

for (int i = 0; i < 2000; ++i){
    mensaje[i]="0";

Make all the text strings in your formation mensaje contain the string "0" , you could have achieved the same effect with a std::vector<std::string> :

std::vector<std::string> mensaje(2000, std::string{"0"});

This other loop:

for (int i = 0; i < 2000; i++){
    cin>>mensaje[i];
    if(mensaje[i]!="0"){
        cout<<"["<<mensaje[i]<<"]";
    }
}

Captures data formatted from the console. The console extraction operator ( >> ) reads data and considers that data will be separated by spaces, what I understand in your question is that you expect the data to be separated by lines and then separate each line by spaces; for that use std::getline :

std::string m;
while (std::getline(std::cin, m)){
    if(m!="0"){
        cout<<"["<<m<<"]\n";
    }
}

With this your output should become:

[... --- ...]
[.--- --- -...  -.. --- -. .  ..--..  ..-. .. -. . -.-.--]

That is not yet what you need; but knowing that the extraction is done considering the space as a separator you could use a std::stringstream about the extracted data :

std::stringstream ss(linea);
if (ss >> linea) {
    std::cout << '[' << linea << ']';
    while (ss >> linea)
        std::cout << "[ ][" << linea << ']';
    std::cout << '\n';
}

If you also trust the limit of input data (instead of checking that you have read "0" ), you eliminate the unnecessary variables and forget your (unnecessary) training of 2000 strings your code could look like this:

#include <iostream>
#include <string>   // string.h no es una cabecera de C++, usa string
#include <sstream>

int main()
{
    int mensajes; // Leer la cantidad de mensajes a recibir
    std::string linea;
    std::cin >> mensajes;
    std::cin.ignore();

    // Por cada mensaje esperado, leemos una línea
    for (int mensaje = 0; (mensaje != mensajes) && std::getline(std::cin, linea); ++mensaje) {
        std::cout << "Message #" << (mensaje + 1) << '\n';

        // Pasamos la línea a un stream de texto
        std::stringstream ss(linea);
        // Leemos dato a dato hasta llegar a final del stream de texto
        if (ss >> linea) {
            std::cout << '[' << linea << ']';
            while (ss >> linea)
                std::cout << "[ ][" << linea << ']';
            std::cout << '\n';
        }
    }

    return 0;
}

You can see the code working in Wandbox 三 へ (へ ਊ) へ ハ ッ ハ ッ .

    
answered by 31.03.2018 / 12:44
source