473,624 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using getchar and putchar

13 New Member
Write a program that counts number of braces, brackets and parenthesis in
the standard input and tests where there are the,
same number of left braces, { , as right braces, } ;
same number of left brackets, [ , as right brackets, ] ;
same number of left parentheses, ( , as right parentheses, )

i am required to use the getchar and putchar funtions......b ut dont really understand how to use them, what would the code look like for one of the requirements?
Feb 14 '07 #1
7 6581
sicarie
4,677 Recognized Expert Moderator Specialist
Write a program that counts number of braces, brackets and parenthesis in
the standard input and tests where there are the,
same number of left braces, { , as right braces, } ;
same number of left brackets, [ , as right brackets, ] ;
same number of left parentheses, ( , as right parentheses, )

i am required to use the getchar and putchar funtions......b ut dont really understand how to use them, what would the code look like for one of the requirements?
Here is a pretty good reference on getchar(), and here is one for putchar().
Feb 14 '07 #2
hey77
13 New Member
i got ti working just fine except now im having an issue
towards the end I need to declare whether the { and } are equal and not equal etc..... here is my code. everytime it says they are equal even if they are not.... what is wrong with the lastfew lines of code?
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int x, a = 0, b = 0,c = 0,d = 0,e = 0,f = 0;
  6.     cout << " Enter characters in the standard input" <<endl;
  7.     while (( x = getchar() ) != EOF )
  8.     {
  9.         putchar (x);
  10.         if (x == '{' )
  11.             a=a++;
  12.         else if (x == '}' )
  13.             b=b++;
  14.         else if (x == ']' )
  15.             c=c++;
  16.         else if (x == '[' )
  17.             d=d++;
  18.         else if (x == '(' )
  19.             e=e++;
  20.         else if (x == ')' )
  21.             f=f++;
  22.         }
  23.         cout << endl << "number of occurrences of { is " << a << endl;
  24.         cout << endl << "number of occurrences of } is " << b << endl;
  25.         cout << endl << "number of occurrences of ] is " << c << endl;
  26.         cout << endl << "number of occurrences of [ is " << d << endl;
  27.         cout << endl << "number of occurrences of ( is " << e << endl;
  28.         cout << endl << "number of occurrences of ) is " << f << endl;
  29.         if ( a=b)
  30.             cout << "{ and } are equal!" <<endl;
  31.         else if ( a !=b )
  32.             cout << "{ and } are not equal!" << endl;
  33.         if ( c=d)
  34.             cout << "[ and ] are equal!" <<endl;
  35.         else if (c!=d)
  36.             cout << "[ and ] are not equal!" << endl;
  37.         if ( e=f)
  38.             cout << "( and ) are equal!" <<endl;
  39.         else if (e!=f)
  40.             cout << "( and ) are not equal!" << endl;
  41.         return 0;
  42. }
Feb 14 '07 #3
sicarie
4,677 Recognized Expert Moderator Specialist
I believe your problem lies at the end of your code.
Expand|Select|Wrap|Line Numbers
  1.         if ( a=b)
  2.             cout << "{ and } are equal!" <<endl;
  3.         else if ( a !=b )
  4.             cout << "{ and } are not equal!" << endl;
  5.         if ( c=d)
  6.             cout << "[ and ] are equal!" <<endl;
  7.         else if (c!=d)
  8.             cout << "[ and ] are not equal!" << endl;
  9.         if ( e=f)
  10.             cout << "( and ) are equal!" <<endl;
  11.         else if (e!=f)
  12.             cout << "( and ) are not equal!" << endl;
  13.         return 0;
  14. }
You use single '=' several times. For conditional statements, you need '=='. The single is an assignment, and will give c d's value, and give e the value in f, etc...
Feb 14 '07 #4
hey77
13 New Member
you are exactly right.......tha nks for your help!
Feb 14 '07 #5
sicarie
4,677 Recognized Expert Moderator Specialist
you are exactly right.......tha nks for your help!
Any time, glad I could help!
Feb 14 '07 #6
anel
1 New Member
I had the same problem,but because of you guys i understand what was going wrong,thnx a lot,both of you...
Mar 30 '07 #7
sicarie
4,677 Recognized Expert Moderator Specialist
I had the same problem,but because of you guys i understand what was going wrong,thnx a lot,both of you...
Glad we could help, anel - please feel free to post if you run into any other problems.
Mar 30 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
6151
by: Yandos | last post by:
Hello all, I have maybe a trivial question, but I cannot think out what is wrong :( How do i detect EOF correctly when i read from stdin? Am I doing it wrong? <pipetest.c> #include <stdio.h> int main(void) { char ch;
42
9868
by: Prashanth Badabagni | last post by:
Hi, Can any body tell me how to print "hello,world" with out using semicolon Thanks in advance .. Bye Prashanth Badabagni
17
4805
by: Martin Jørgensen | last post by:
Hi, Since I'm a newbie I have some small but quick (probably) stupid questions also :-) This is my "get_double" function which takes a default argument also of type double. The function returns a value of type double so I can write something like value = getdouble(0.5); in my program. --------------
1
1516
by: yky | last post by:
in this programme,if i input a number 100,the program will be over ,and i donot have the chance to input a char to z; # include<stdio.h> main() { int x; char z; do {printf("please input a number between 1 and 100\n"); scanf("%d",&x);
14
2850
by: arnuld | last post by:
i have slightly modified the programme from section 1.5.1 which takes the input frm keyboard and then prints that to the terminal. it just does not run and i am unable to understand the error message. may you tell me what is wrong and how to make that right ? ------------------------------ INPUT --------------------- #include <stdio.h> int main() {
0
1069
by: gilly | last post by:
Hi, Sorry this may sound stupid but i cant figure it out. I was wonder how you use getchar to exit after enter has been pressed a multiple times. This is what i have but as its within a second while loop every it re-enters it reads the last time enter was pressed and exits. I just need it to allow me to re-enter values a second time. while(c! = '\n'){
20
636
by: Senthil-Raja | last post by:
The getchar() function is expected to fetch the next character in the input stream and return it. But, when I wrote a program using this function, it looks like the reading of the input stream happens only after I press the ENTER key. Please someone explain why this is happening. Regards, Senthil-Raja.
2
2004
by: srini4vasan | last post by:
#include <stdio.h> int main() { char n, m; puts (" Enter the first string and . to terminate :"); do { n = getchar(); putchar(n);
8
5264
by: lovecreatesbea... | last post by:
Thank you for your time. #include <stdio.h> int main(void) { int c; while ((c = getchar()) != EOF){
0
8234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8677
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.