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?
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().
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? - #include <iostream>
-
using namespace std;
-
int main ()
-
{
-
int x, a = 0, b = 0,c = 0,d = 0,e = 0,f = 0;
-
cout << " Enter characters in the standard input" <<endl;
-
while (( x = getchar() ) != EOF )
-
{
-
putchar (x);
-
if (x == '{' )
-
a=a++;
-
else if (x == '}' )
-
b=b++;
-
else if (x == ']' )
-
c=c++;
-
else if (x == '[' )
-
d=d++;
-
else if (x == '(' )
-
e=e++;
-
else if (x == ')' )
-
f=f++;
-
}
-
cout << endl << "number of occurrences of { is " << a << endl;
-
cout << endl << "number of occurrences of } is " << b << endl;
-
cout << endl << "number of occurrences of ] is " << c << endl;
-
cout << endl << "number of occurrences of [ is " << d << endl;
-
cout << endl << "number of occurrences of ( is " << e << endl;
-
cout << endl << "number of occurrences of ) is " << f << endl;
-
if ( a=b)
-
cout << "{ and } are equal!" <<endl;
-
else if ( a !=b )
-
cout << "{ and } are not equal!" << endl;
-
if ( c=d)
-
cout << "[ and ] are equal!" <<endl;
-
else if (c!=d)
-
cout << "[ and ] are not equal!" << endl;
-
if ( e=f)
-
cout << "( and ) are equal!" <<endl;
-
else if (e!=f)
-
cout << "( and ) are not equal!" << endl;
-
return 0;
-
}
sicarie 4,677
Recognized Expert Moderator Specialist
I believe your problem lies at the end of your code. -
if ( a=b)
-
cout << "{ and } are equal!" <<endl;
-
else if ( a !=b )
-
cout << "{ and } are not equal!" << endl;
-
if ( c=d)
-
cout << "[ and ] are equal!" <<endl;
-
else if (c!=d)
-
cout << "[ and ] are not equal!" << endl;
-
if ( e=f)
-
cout << "( and ) are equal!" <<endl;
-
else if (e!=f)
-
cout << "( and ) are not equal!" << endl;
-
return 0;
-
}
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...
you are exactly right.......tha nks for your help!
sicarie 4,677
Recognized Expert Moderator Specialist
you are exactly right.......tha nks for your help!
Any time, glad I could help!
I had the same problem,but because of you guys i understand what was going wrong,thnx a lot,both of you...
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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;
|
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
|
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.
--------------
|
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);
|
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() {
| |
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'){
|
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.
|
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);
|
by: lovecreatesbea... |
last post by:
Thank you for your time.
#include <stdio.h>
int main(void)
{
int c;
while ((c = getchar()) != EOF){
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |