reading from several lines

Pages: 12
Nov 8, 2009 at 8:36pm
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


Nov 8, 2009 at 9:25pm
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 Nov 8, 2009 at 9:25pm
Nov 8, 2009 at 10:25pm
@ 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

Nov 8, 2009 at 11:38pm
@Bazzy ... My bad!
Nov 9, 2009 at 1:40am
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 Nov 9, 2009 at 1:41am
Nov 9, 2009 at 1:44am
...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;
}
Nov 9, 2009 at 3:04pm
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
Nov 10, 2009 at 4:12pm
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;
}
Nov 10, 2009 at 4:28pm
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 Nov 10, 2009 at 4:46pm
Nov 10, 2009 at 4:45pm
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;
//... 
Nov 10, 2009 at 4:51pm
ive updated my post before you posted yours so what i should i do now? sorry for any inconvenience
Last edited on Nov 10, 2009 at 4:53pm
Nov 10, 2009 at 4:58pm
Change the if on line 16 to a while and remove the else part.
Then is should work
Nov 10, 2009 at 5:45pm
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;
}
Nov 10, 2009 at 5:54pm
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
Nov 11, 2009 at 1:38am
ive tried some things for the third loop but cant seem to make it and probably wont, so i again call upon your help :(

Nov 11, 2009 at 1:10pm
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 Nov 11, 2009 at 1:12pm
Nov 11, 2009 at 2:06pm
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 Nov 11, 2009 at 2:14pm
Nov 12, 2009 at 9:17am
Can you post your complete source?
It works for me
Nov 12, 2009 at 6:49pm
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;
}
Nov 12, 2009 at 7:04pm
If I test it it works, What input did you give?
Pages: 12