Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading string with spaces in c++

Tags:

c++

How can I read input line(type string) with whitespace? I tried getline but it goes into infinite loop. Following is my code.

#include <iostream>
#include <cstring>

#define MAX 50 //size of array

//Used G++ 4.6.3 compiler
using namespace std;

int main() {

struct Manager {
string name;
int age;
int working_years;
string phone;
int salary;
}info[MAX];

char inp; //To choose options
int array_pos = 0; //Current position in array of Manager structure
string search_name; //Manager name you want to search

cout << "Press 'i' to insert manager information or 's' to search for manager information by name or 'a' to abort: ";
cin >> inp;

while(inp != 'a') {
int search_num = 0; //array position at which search result is found
int found = 0;
if (inp == 'i' || inp == 's') {
    if (inp == 'i') {
        int k = array_pos;
        cout << "Enter the information of the manager no "<<k+1<<" is : "; 

        cout << "Enter the Name : "; 
                     //infinte loop occurs
        getline(info[array_pos].name, '\n');
        //cin >> info[array_pos].name;

        cout<<"Enter manager age : "; 
        cin >> info[array_pos].age;

        cout << "Enter manage working years : ";
        cin >> info[array_pos].working_years;

        cout << "Enter manager phone no. : ";
        cin >> info[array_pos].phone;

        cout << "Enter manager salary : ";
        cin >> info[array_pos].salary;
        array_pos++;
    }
    if (inp == 's') {
        cout << "Enter the manager name you want to search : ";
        cin >> search_name;
        for(int i = 0; i < array_pos; i++) {
            //using str1.compare(str2) to compare manager name
            if(info[i].name.compare(search_name) == 0) { //manager name found in array of structure
                found = 1;                  
                search_num = i;                 
                cout << "Name : " << info[search_num].name << "\n";
                cout << "Age: " << info[search_num].age << "\n";
                cout << "Working Years: " << info[search_num].working_years << "\n";
                cout << "Phone No. : " << info[search_num].phone << "\n";
                cout << "Salary : " << info[search_num].salary << "\n";
            } //end of if loop for comparing string
        } //end of for loop for searching
        if(found == 0)
            cout << "No Manager by this name exist in record" << "\n"; 

    } //end of if loop

} //end of if loop for  searching or insertion
if(inp == 'a')
    break;

cout << "Press 'i' to insert manager information or 's' to search for manager information by name or 'a' to abort: ";
cin >> inp;
} //end of while loop

return 0;
}
like image 547
prattom Avatar asked Oct 20 '13 11:10

prattom


People also ask

How do you read a string with spaces?

So we use “%[^\n]s” instead of “%s”. So to get a line of input with space we can go with scanf(“%[^\n]s”,str);

How do I check if a string contains spaces in C?

The isspace() function checks whether a character is a white-space character or not. If an argument (character) passed to the isspace() function is a white-space character, it returns non-zero integer. If not, it returns 0.

Does scanf read spaces in C?

scanf() reads input until it encounters whitespace, newline or End Of File(EOF) whereas gets() reads input until it encounters newline or End Of File(EOF), gets() does not stop reading input when it encounters whitespace instead it takes whitespace as a string.

How do I make scanf read whitespace?

The secret to getting scanf to perform this way is to put a blank in the format string before the %c format specifier. The blank tells scanf to skip white space and it will actually skip any number of white space characters before reading and storing a character.


1 Answers

"How can I read input line(type string) with whitespace?"

std::string line;
if (std::getline(std::cin, line)) {
    ...
}

Note that apart from checking the return value of std:getline call, you should also avoid mixing >> operator with std::getline calls. Once you decide reading the file line by line, it seems to be cleaner and more reasonable to just make one huge loop and do the additional parsing while using string stream object, e.g.:

std::string line;
while (std::getline(std::cin, line)) {
    if (line.empty()) continue;
    std::istringstream is(line);
    if (is >> ...) {
        ...
    }
    ...
}
like image 150
LihO Avatar answered Sep 21 '22 14:09

LihO