LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2008, 02:45 AM   #1
lord_didger
Member
 
Registered: Jun 2005
Location: Poland
Distribution: Debian
Posts: 106

Rep: Reputation: 15
reading from stdin


I want to make a calculator. It aim is to perform the simplest arithmetic operations. It expects a few lines similiar to this one '+ 8 6'. Each line is one task. The program reads the line, do the task and put the result to stdout.
Everything works but the program doesn't stop. I would like the programm read what is currently in buffer, did the tasks and end (not waiting).
How can I do it? The code is:

Code:
#include <iostream>
using namespace std;

int main()
{
  ios_base::sync_with_stdio(0);
  int a,b;
  char ch;
 
  while(!cin.eof())
  {
    cin >> ch >> a >> b;
    
    int wynik;
    switch(ch)
    {
      case '+':
        wynik = a+b;
        break;
      case '-':
        wynik = a-b;
        break;
      case '*':
        wynik = a*b;
        break;
      case '/':
        wynik = a/b;
        break;
      case '%':
        wynik = a%b;
        break;
    }
    
    cout << wynik << endl;
    cin.ignore();
  }
 
  system("PAUSE");
  return(0);
}

Last edited by lord_didger; 01-16-2008 at 09:30 AM. Reason: not enough information
 
Old 01-14-2008, 07:08 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
What does system("PAUSE"); do? Try taking it out of the program.

--------------------
Steve Stites
 
Old 01-15-2008, 09:51 AM   #3
lord_didger
Member
 
Registered: Jun 2005
Location: Poland
Distribution: Debian
Posts: 106

Original Poster
Rep: Reputation: 15
Actually, 'system("PAUSE")' means nothing. I have written the program in Windows where this function is available. Nevertheless problem remains.
 
Old 01-15-2008, 10:06 AM   #4
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Your program will stop if you press Ctrl+D, signifying the cin.eof(), which is the stopping condition of your while loop.
I don't really understand the question. If you want your program to evaluate just one expression and then stop, why did you put in a while loop in the first place?
 
Old 01-15-2008, 12:51 PM   #5
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Agreeing with Uncle Theodore, if you want the program to end, take out the WHILE loop and replace with:
Code:
if (cin.eof())
  return(-1); 
  /* Or some other number that specifies 
     end of operation without failure */
else
{
  switch (ch)
  {
    ...
    ...
    ...
  }
}
 
Old 01-16-2008, 09:27 AM   #6
lord_didger
Member
 
Registered: Jun 2005
Location: Poland
Distribution: Debian
Posts: 106

Original Poster
Rep: Reputation: 15
I have already solved the problem.

The program's aim was to work as a calculator - it should read some data from stdin, execute the data and exit. When I type in command line 'program < data_file' I expect to get some information on stdout and the program to exit.

The main point is that I don't how much tasks there are on stdin

The code that lives up my expectations:

Code:
#include <iostream>
using namespace std;

int main()
{
  ios_base::sync_with_stdio(0);
  int a,b;
  char ch;
 
  while(cin >> ch >> a >> b)
  //while(!cin.eof())
  {
    //cin >> ch >> a >> b;
    int wynik;
    switch(ch)
    {
      case '+':
        wynik = a+b;
        break;
      case '-':
        wynik = a-b;
        break;
      case '*':
        wynik = a*b;
        break;
      case '/':
        wynik = a/b;
        break;
      case '%':
        wynik = a%b;
        break;
    }
    
    cout << wynik << endl;
    cin.ignore();
  }
 
  return(0);
}
and example of content of data_file

Quote:
+ 7 9
- 0 4
* 5 6
/ 8 3
% 5 2
and that is all.

Last edited by lord_didger; 01-16-2008 at 09:30 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with STDIN. dthai Linux - General 1 09-15-2006 02:22 AM
Reading from stdin till eof stuck when sending multiple commands fredyk_ Linux - Software 1 04-25-2006 02:23 AM
stdout stdin Furlinastis Linux - Newbie 3 08-11-2005 11:00 PM
java - reading from stdin greg108 Programming 4 01-13-2005 03:29 PM
stdin and NUL. How ? Mike Davies Linux - Software 5 10-28-2004 07:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration