user input, ignore space array of strings

Sorry if this has been asked before, I did search and found similar questions but none with an array of strings.

I have a dynamically allocated array of strings, and I am trying to receive user input ignoring the whitespace. I have tried getline(cin,StringArray[0]); and this does not work, I have also tried is using a pointer rather than accessing the array through brackets.

1
2
3
4
string *billName;
    cin>>size;
    billName   = new string[size];
    getline(cin,billName[0]);
billname is a pointer, not an array. It cannot be accessed with a subscript operator.

Why don't you use
1
2
string[10] billname;
getline(cin,billname[0]);


EDIT:
If the above statement is foolish, ignore it. I just reallized this might not be what you want.
Last edited on
@Superdude:
Actually, TC's syntax is correct. TC is simply using dynamic memory to allocate an array of an arbitrary size.

@TC:
There isn't a standard function to just ignore all whitespace, though you could write a function to go through the string after you have read it and remove any characters you don't like.
l00kadiversion wrote:
and I am trying to receive user input ignoring the whitespace.


Could you explain what you mean by that? Preferably an example input and what you would like extracted.
Not sure what you mean by ignoring white space. White space before the input, in the input or after the input???

This getinput function returns input until the EOF character

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdio>
#include <cctype>
#define MAXWORDS 100

int getInput(char *input, int=MAXWORDS);

int main() {
	char words[MAXWORDS];
	while (getInput(words));
	return 0;
}

int getInput(char *input, int maxlen) {
	int d;
	char * w = input;
	while (isspace(d=getchar())) //ignore space
	;
	if ( d == EOF ) {
		*w = '\0';
		return 0;
	}
	
	for ( *w++ = d ; --maxlen ; *w++ = d )
		if ( (d=getchar()) == '\n' or d == EOF ) break;
	*w = '\0';
	return *input;
}
Basically I'm trying to ignore the spacing between words. It's going to be a monthly bill program, and I'm trying to accommodate for bills that are more than one word, ie: Capital One. Thanks for all of your input so far.
You were on the right track then. Why wasn't the code in the original post working for you?

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    std::string name ;
    std::cout << "Enter bill name: " ;
    std::getline(std::cin, name) ;
    
    std::cout << "Bill name is \"" << name << "\"\n" ;
}


http://ideone.com/pLcdyB
Last edited on
Hey Cire, thanks. I believe my issue to be because I am trying to getline an array using subscripts. I may need to try it as a pointer rather than using the subscripts, since that is when it does not work for me.
Given the code in the original post, I suspect your code suffers from another issue, namely that of mixing unformatted and formatted input. After you do cin >> size; a newline remains in the input stream that is encountered by the call to getline, causing the extraction of an empty string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <iostream>
 
int main()
{
    unsigned nBills;
 
    std::cout << "How many bills will you enter?\n";
    std::cin >> nBills;
 
    std::string* bills = new std::string[nBills];
 
    std::cout << "Please enter the bill names.\n";
 
    std::cin >> std::ws ;  // Remove leading whitespace.
    for ( unsigned i=0; i<nBills; ++i )
        std::getline(std::cin, bills[i]);
 
    std::cout << "The bills you entered are:\n";
    for (unsigned i = 0; i < nBills; ++i)
        std::cout << '\t' << bills[i] << '\n';
}


Note line 15.

http://ideone.com/yCA4dB

Thank you very much cire, that worked perfectly.
Topic archived. No new replies allowed.