I asked this before but didn't get an answer so I went to talk to my "professor" and he just about made me want to quit programming :( . So a little help would go great lenghs to salvaging this day.

Basically I have to right a program that reads stock transactions and then adds them to queue or sells them, simple enough. The problem is that I need set up some kind of a loop that will read in all the transactions. I have plenty of experince doing this with files by running till EOF. But I am not allowed to use file streams. All input will be through cins and the input will come through file redirection. Again I could do this but I will not know the number of transactions that will be entered. So how can I setup a program that will read all the input when the number of records is unknown? Any help would be greatly appreciated. Thanks.

Can you use a sentinal value, like ask the user to enter values and q to quit sort of thing?

I don't think so. There will be no input from the user exactly. He will run the file using file redirection (a.out < test.txt, for example) and there will be no interaction between him and the program. All the input will come from file but for some reason I can't use file streams. And to the best of my knowledge there will be no character in the file that will signal the end. I am kind of at loss here. I don't know how to run this loop that will get info from cins without crashing the program when it tries to read records that aren't there. I asked if I could at least open a file stream so that I could test for EOF and he basically called me stupid. :mad:

Show your code. Your preferred method can probably be very easily adapted, but I can't read your mind and see the code you have.

This is a short example of what you could do.

#include <iostream>
 
 int main()
 {
    std::string text;
    while(std::cin >> text)
    {
 	  std::cout << "text: " << text << std::endl;
    }
    return 0;
 }

Feeding this source code to its resulting executable, I get this.

C:\Test>testpp < testpp.cpp
text: #include
text: <iostream>
text: int
text: main()
text: {
text: std::string
text: text;
text: while(std::cin
text: >>
text: text)
text: {
text: std::cout
text: <<
text: "text:
text: "
text: <<
text: text
text: <<
text: std::endl;
text: }
text: return
text: 0;
text: }

Thanks a bunch. Just to make sure I understand the concept the line.....

while(string >> text)

...will run until there is no more available input without crashing the program. Thats all I was trying to ask this "teacher" and he started yelling how I was taking a computer science class and not a philosphy class. Oh, and also that when someone wants a variable to hold a number they should create an int. :(

#include <iostream>
 
 int main()
 {
    std::string text;
    while(std::cin >> text)
    {
 	  std::cout << "text: " << text << std::endl;
    }
    return 0;
 }

QUOTE]

Thanks a bunch. Just to make sure I understand the concept the line.....

while(string >> text)

...will run until there is no more available input without crashing the program.

When there is no more input available, the >> operator fails to input a string and returns a zero value that breaks the loop.

http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.4

Another example:

1 2 3 4 5
6 7 8 9 10
11 12

#include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main()
    {
   int value, sum = 0;
   while ( cin >> value )
   {
    	  sum += value;
    	  cout << "value = " << value << ", sum = " << sum << endl;
   }
   return 0;
    }

C:\Test>testpp < file.txt
value = 1, sum = 1
value = 2, sum = 3
value = 3, sum = 6
value = 4, sum = 10
value = 5, sum = 15
value = 6, sum = 21
value = 7, sum = 28
value = 8, sum = 36
value = 9, sum = 45
value = 10, sum = 55
value = 11, sum = 66
value = 12, sum = 78

Thanks a lot Dave, my program runs perfectly now. Its nice to know I have a place I can ask questions without being ridiculed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.