Character I/O, and inputting spaces.

Pages: 123
Basically, what I want to be able to do is input this:
1 243 345 64564 567
Note: there are 5 spaces in total between the 1 and the 7.
And have it output this:
1 _ _ _ _ _ 7
The _'s denote spaces.

So, enter a character and have my program ignore everything between the first and last character until 5 spaces have been input.

This is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>

using namespace std;

int main (void)
{
    char input;

    cout << "Enter a character: " << endl;
    cin.get(input);

    cout << input;
}


I tried doing
cin.get(input) >> input >> input >> input >> input >> input;
but when the program would cout, it would ignore everything but the last number.

Help please!
Last edited on
Are you required to use char?

Strings would be more like what you tried:
1
2
3
4
5
6
7
8
char start, end;
string first, junk, last;

cout << "Enter the line: ";
cin >> first >> junk >> junk >> junk >> last;

start = first.at(0);
end = last.at(last.length() - 1);
Yeah, we're required to use char. I'm just doing this for 'fun', and since in class we haven't gone over strings yet I don't want to start using it.
Help, please? I just want to get moving in the right direction..
It seems as though no one ever posts to help out in my threads lol
I would seriously inquire to your professor about being able to use strings it is pretty simple that way (really simple) and defiantly a lot more efficient as to do it this way maybe using the find function in a for loop?
Isn't the point here that it's not quite so simple??

Step one: can you read a line of text (any single line!) char by char from cin and write it back out, char by char, using cout?

Andy

PS I would use cin.get() rather than cin >>
Last edited on
Sorry, just got out of work.
Yeah, we're required to use char.


Well that makes this much more fun :)

andywestken's gives a good first step, but it does raise another question: Can the input have more than 5 spaces, or should the program stop reading after the 7th word (a word being a string of chars delimited by a space)?
Last edited on
@mike12255: Well, that's the thing. I know how to do it using strings, because I'm a ways further down the book, but I don't know how to do it using chars.

And I definitely know that a loop would be best, but I don't know how to make the program say "read only the first and last character of everything I have input".

@andywestken: I'm definitely using cin.get() rather than cin, and I can make it read back to me any particular character I have input, but it'll only read back to me one character.

Ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>

using namespace std;

int main (void)
{
    char input;

    cout << "Enter a character: " << endl;
    cin.get(input) >> input >> input;

    cout << input;
}


Depending on the number of inputs I use, that's the character I'll get back.

@LowestOne: the way the problem is worded is a little weird. It says I can input as many characters as I want, but have the program output only the first and last character, with 5 spaces in between.
Yes, you need a loop.

And while you can't make the program read just the first and last, but you can read them all but only output the first and last!!

Andy

PS Should it always be 5 spaces? Or do you just need to o/p a _ for each space you find?
Last edited on
And how would I go about doing that :D
Why not try??

Andy

PS Prob. easiest with while
Last edited on
I've been trying but I don't know what argument to make in the while loop :\

And yes, it should always be 5 spaces.
The book gives some sample output:

1
2
3
4
5
6
7
8
9
10
11
12
Enter a character followed by 5 spaces followed by a character:
1____3444_ a
You entered:
1_____a

Enter a character followed by 5 spaces followed by a character:
bcsa_a
_
___d
_c
You entered:
b_____c


Where _____ denotes a space. It wouldn't actually show up like that in the code, but if I just left it blank on here it wouldn't show the spaces.
So you've tried something along the lines of

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>

using namespace std;

int main (void)
{
    char input;

    cout << "Enter a string: " << endl;

    while(true) // this "prob" won't stop so you'll have to kill program
    {
        cin.get(input);

        cout << "\"" << input << "\" "; // quote chars to make them stand out!
    }

    return 0;
}


to see if you can spot how to spot when things should stop?

Andy
Last edited on
See, I thought about doing that but then I thought: how would it know when to stop if the loop is set to true? So I just didn't bother to.

However, I figured out what the argument should be.
(while != //5 spaces)
But I don't know how to rephrase that into code because I don't know what a 'space' would be called. Does that make sense?
You can't terminate based on the number spaces as you don't know how any chars come after the last one of them. So how would find the '7'?

Sorry, I am not sure what you mean by "what a 'space' would be called".

Don't you have to count them?

Andy
Last edited on
Well, I need to count spaces. What I mean is how would I write that?
The loop would look something like this:
while (spaceCounter != 5)

Anyway, this is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    char next;
    int count = 0;
    cout << "Enter a line of input:\n";
    cin.get(next);

    while (next != '\n')
    {
        if (count == 0)
        cout << next;
        count++;
        cin.get(next);
    }
}


There's still a lot of problems with this, though, heh.
Last edited on
1
2
3
4
5
6
7
8
int amountOfSpaces = 0;
char ch;
while (amountOfSpaces != 5)
{
  ch = cin.get();
  if (ch == ' ')
    amountOfSpaces++;
}

I had something similar to that, but it's not displaying anything in the console now. I'm so frustrated

Edit: Even with the counter, though, I don't know how to display the very last character of whatever I input into the console.
Last edited on
Okay, what if the user enters this:
1
2
3
4
5
6
7
8
9
1  // easy to extract, the first character
12_34_56_7_8_  // my loop extracts this
___

...

2

..

What character do you want there? The new line after the 5th space, the space after the 5th space, the last period in line 5, the 2, or the last period in line 9?
Last edited on
Pages: 123