reading from several lines

Pages: 12
hi,

i was wondering how to make a program, that would read numbers(which user would input) and for example output how many of them user inputed - but the point is to make such a program, in which lines and speces would be ignored (for example, between two numbers there would be 3 lines inbetween, and 7 spaces between the second and third...). Numbers can have several digits.

i would like to know, how to make it work like: "when i struck a number, i will read all of its digits and mark than as 1 number and then search for the next one"
(just want to make sure that you understand)

ty in advance


for example output how many of them user inputed

You mean like '3455678' - there are two 5's here?

there would be 3 lines inbetween

I would like to see you get user input from the console where the user can put lines between there text without pressing enter!
Last edited on
@ mcleano
You mean like '3455678' - there are two 5's here?
The OP said:
between two numbers there would be 3 lines inbetween, and 7 spaces between the second and third...).
Numbers can have several digits.


@ Claimz
What you are describing is exactly how the >> operator works, reads stuff discarding all spaces/new lines.
Using >> with cin is not really good, for a better solution see http://www.cplusplus.com/forum/articles/6046/
You would need a loop to read all the number found in a line from the stringstream

@Bazzy ... My bad!
yes i know all of that, but my quastion is, how to make it (i hope im not asking too much!)
how to read first number and then we can do what ever we want with it, and then move onto another - that is, how to convert from string to integer all the numbers(seperated with as many spaces as you you want) that have been inputed, particulary, how to make that loop of input type getline()'s

my approach would be to ignore all the spaces at beginning(it there were any) until id encounter a number, and read it until next space (though i dont know how i would manage more then one digit long numbers!!!), and so forth

what would you do?

ty
Last edited on
...i want to make a c++ version of this c program:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main() {
int howmanytimes = 0, lowest= 0, integer = 0;
while (1 == scanf("%d", &integer))
{
if (howmanytimes == 0 || integer < lowest) lowest= integer , howmanytimes = 1;
else if (integer == lowest) howmanytimes ++;
}
printf("%d\n", howmanytimes ); return 0;
}
You can replace the while condition with cin >> integer which will return true if the input was OK.
Then you should go for the getline way, you would need two loops for that: the first reads the lines from cin, the second reads the integers from the line
ive managed without getline because of the while loop (i think)
but i wouldnt done it without your help! ty

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main() {
int howmanytimes = 0, lowest= 0, integer = 0;
while (cin >> integer)
{
if (howmanytimes == 0 || integer < lowest) lowest= integer , howmanytimes = 1;
else if (integer == lowest) howmanytimes ++;
}
cout << howmanytimes << endl; return 0;
}
how would you do the same with getline? i did it like so and it wont work properly:

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
28
29
30
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {
int howmanytimes = 0, lowest= 0, integer = 0;
string input = "";




while (getline(cin, input))
{
stringstream myStream(input);
if (myStream >> integer)
{
if (howmanytimes == 0 || integer < lowest) lowest= integer , howmanytimes = 1;
else if (integer == lowest) howmanytimes ++;
}

else 
{
cout << howmanytimes << endl; 
break;
}

}
 return 0;
}
Last edited on
Add another loop:
1
2
3
4
5
6
7
8
9
10
// ... same until line 9
while ( true ) // continuous loop
{
    getline ( cin, input ); // read a line
    if ( !cin ) // you may want also to check input == "exit" or something like that to terminate your program 
       break; // exit loop if input failed 
    // from line 10 to line 16 
}
cout << howmanytimes << endl;
//... 
ive updated my post before you posted yours so what i should i do now? sorry for any inconvenience
Last edited on
Change the if on line 16 to a while and remove the else part.
Then is should work
ok it works now, just have one more quastion:
- how to make the program recognize "exit" even if its not typed at the beginning of a line?

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
28
29
30
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {
int howmanytimes = 0, lowest= 0, integer = 0;
string input = "";


while (getline(cin, input))
{
stringstream myStream(input);
while(myStream >> integer)
{
if (howmanytimes == 0 || integer < lowest) lowest= integer , howmanytimes = 1;
else if (integer == lowest) howmanytimes ++;
}


if (input == "exit")
break;


}
cout << howmanytimes << endl; 


 return 0;
}
Add these two lines before line 21:
1
2
3
myStream.clear(); // set stream state to good ( you are out the inner loop so previous input failed )
myStream >> input; // read a string from the stream
// ... check it ... 

Sample inputs:

some valid numbers ... "exit"         Will  work
"exit" any input                      Will  work
valid numbers..invalid input.."exit"  Won't work

For the third case you would need a third loop
ive tried some things for the third loop but cant seem to make it and probably wont, so i again call upon your help :(

It may look ugly but this should work:

19
20
21
22
23
24
        myStream.clear();

        while ( myStream >> input && input != "exit" ); // read until failure or input == "exit"

        if (input == "exit")
            break; // exit from the main loop 
Last edited on
ive tried that already with no luck...
it only works if failed inputs were typed in together at the end, before the "exit" input
Last edited on
Can you post your complete source?
It works for me
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
28
29
30
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {
int howmanytimes = 0, lowest= 0, integer = 0;
string input = "";

while (getline(cin, input)) 
{
stringstream myStream(input); 
while(myStream >> integer)
{
if (howmanytimes == 0 || integer < lowest) lowest = integer , howmanytimes = 1;
else if (integer == lowest) howmanytimes ++;
}

myStream.clear();

while ( myStream >> input && input != "exit" ); 

if (input == "exit")
    break; 

}
cout << howmanytimes << endl; 

 return 0;
}
If I test it it works, What input did you give?
Pages: 12