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>
usingnamespace 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.
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?
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)?
@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>
usingnamespace 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.
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.
#include <iostream>
#include <fstream>
usingnamespace 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?
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?
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?