473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Using getchar and putchar

13
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......but dont really understand how to use them, what would the code look like for one of the requirements?
Feb 14 '07 #1
7 6569
sicarie
4,677 Expert Mod 4TB
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......but 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
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 Expert Mod 4TB
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
you are exactly right.......thanks for your help!
Feb 14 '07 #5
sicarie
4,677 Expert Mod 4TB
you are exactly right.......thanks for your help!
Any time, glad I could help!
Feb 14 '07 #6
anel
1
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 Expert Mod 4TB
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
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>...
42
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
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...
1
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...
14
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...
0
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...
20
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...
2
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
by: lovecreatesbea... | last post by:
Thank you for your time. #include <stdio.h> int main(void) { int c; while ((c = getchar()) != EOF){
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.