LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-21-2003, 09:46 PM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
Why the "cout" is invoked twice?


I think the "cout" will be invoked once when the loop is invoked once. However the "cout" is invoked twice when the loop is invoked once.
Code:
#include<iostream>
using namespace std;
//a macro to define dummy functions:
#define DF(N) void N() {cout<<"function"#N"called..."<<endl;}
DF(a); DF(b); DF(c);
void (*func_table[])()={a, b, c}; //define and initialize an array of pointers to functions
int main ()
{
   while(1) {
      cout<<"press a key from 'a' to 'b' or 'q' to quit"<<endl;
      char c;
      cin.get(c);
      if (c=='q')
         break; //...out of while (1)
      if (c<'a' || c>'g')
         continue;
      (*func_table[c-'a'])(); //dereferencing calls the function
   }
}
Here is the result:
[root@localhost abide]# g++ fun_poi_c.cpp -o fun_poi_c
[root@localhost abide]# ./fun_poi_c
press a key from 'a' to 'b' or 'q' to quit
a
functionacalled...
press a key from 'a' to 'b' or 'q' to quit
press a key from 'a' to 'b' or 'q' to quit
b
functionbcalled...
press a key from 'a' to 'b' or 'q' to quit
press a key from 'a' to 'b' or 'q' to quit
q
[root@localhost abide]#

What is the wrong? Thank you.

Last edited by Xiangbuilder; 09-21-2003 at 09:48 PM.
 
Old 09-22-2003, 12:26 AM   #2
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
well i just played with it, and my guess is because cin.get () leaves garbage in the buffer. placing a cin.ignore (); after the cin.get (c); line makes it work as you expect it to work.



Code:
#include<iostream>
using namespace std;
//a macro to define dummy functions:
#define DF(N) void N() {cout<<"function"#N"called..."<<endl;}
DF(a); DF(b); DF(c);
void (*func_table[])()={a, b, c}; //define and 
                                 //initialize an array 
                                 //of pointers to functions
int main ()
{
   while(1) {
      cout<<"press a key from 'a' to 'b' or 'q' to quit"<<endl;
      char c;
      cin.get(c);
      cin.ignore ();
      if (c=='q')
         break; //...out of while (1)
      if (c<'a' || c>'g')
         continue;
      (*func_table[c-'a'])(); //dereferencing calls the function
   }
}
output:

Code:
[vjong@Tron temp]$ ./test.exe
press a key from 'a' to 'b' or 'q' to quit
a
functionacalled...
press a key from 'a' to 'b' or 'q' to quit
b
functionbcalled...
press a key from 'a' to 'b' or 'q' to quit
q
[vjong@Tron temp]$

Last edited by megaspaz; 09-22-2003 at 12:28 AM.
 
Old 09-22-2003, 01:08 AM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you. It works well.
I don't know there is command, cin.ignore(), thank you tell me that. This below works well also:
Code:
#include<iostream>
using namespace std;
//a macro to define dummy functions:
#define DF(N) void N() {cout<<"function"#N"called..."<<endl;}
DF(a); DF(b); DF(c);
void (*func_table[])()={a, b, c}; //define and initialize an array of pointers to functions
int main ()
{
   while(1) {
      cout<<"press a key from 'a' to 'b' or 'q' to quit"<<endl;
      char c, cr;
      cin.get(c); 
      cin.get(cr); //second one for CR
      if (c=='q')
         break; //...out of while (1)
      if (c<'a' || c>'g')
         continue;
      (*func_table[c-'a'])(); //dereferencing calls the function
   }
}
The code is from <<thinking in c++>>,

Last edited by Xiangbuilder; 09-22-2003 at 01:20 AM.
 
Old 09-22-2003, 02:28 AM   #4
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
well the only real time i've ever seen a need for cin.ignore was if you were mixing cin with cin.getline where cin.ignore () would be the line right after the last cin before any getline line.

ie:
char c;
char str[5];

cin >> c;
cin.ignore ();
cin.getline (str,4);
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
what is "sticky bit mode" , "SUID" , "SGID" augustus123 Linux - General 10 08-03-2012 04:40 AM
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
cout << "Hello!!" dan245 LinuxQuestions.org Member Intro 1 11-15-2005 04:47 PM

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

All times are GMT -5. The time now is 05:31 AM.

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