Thread: IF - problem

  1. #1
    Enabled to talk to you!!! Bilal_Rajput's Avatar
    Join Date
    Oct 2005
    Location
    .:L.A.H.O.R.E:. >>> .:P.A.K.I.ST.A.N:.
    Posts
    5

    Arrow IF - problem

    Code:
     #include<iostream.h>
     #include<conio.h>
     int main()
     {
     clrscr();
     int grade,total=0;
     float avg,a=1;
     char z,y,n;
     cout<<"Enter the grade of the student = ";
     cin>>grade;
     do
     {
     cout<<"\nDo you want to enter another grade? (y/n) - ";
     cin>>z;
     if ( z == y )
     {
     cout<<"\nEnter the grade of the student = ";
     cin>>grade;
     }
     if ( z == n )
     {
     cout<<"\nAverage of the student = "<<avg<<endl;
     getch ();
     return 0;
     }
     total=total+grade;
     avg=total/a;
     a++;
     }while ( a == a );
     cout<<"\nAverage of the student = "<<avg<<endl;
     getch ();
     return 0;
     }

  2. #2
    Enabled to talk to you!!! Bilal_Rajput's Avatar
    Join Date
    Oct 2005
    Location
    .:L.A.H.O.R.E:. >>> .:P.A.K.I.ST.A.N:.
    Posts
    5
    In this program..................the problem i am facing is that.............*z* do stores the user entered character in it like *y* or *n* ................but *if (z==y) and if(z==n) isnt working......plz help me.............plzzzzzzzzzzzz

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The easy way to do it is to compare the variable z with the character literal 'y' or 'n'. Remove the variables called y and n and use 'y' and 'n' in your if statements.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Also, you did'n initialise the variabels y and n. I assume you want y to be 'y' and n to be 'n', so initialise like this:

    y = 'y';
    n = 'n';

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    ...and this is a minor issue, but it's proper to cover both cases when using user input in an if statement.

    Try this:
    Code:
     if ( z == 'y' || z == 'Y' )
     {
         // CODE
     }
     if ( z == 'n' || z == 'N' )
     {
         // CODE
     }
    Secondly, you have no reason to use conio.h in your program. Since your not looping, the clrscr() shouldn't be doing anything at the beginning of your program and the getch() can be replaced with cin.get(), which is found in iostream.h.
    Last edited by SlyMaelstrom; 10-13-2005 at 02:34 PM.
    Sent from my iPad®

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Good advice, except you'd need two cin.get() calls (or a cin.ignore() and a cin.get()) because of the leftover newline.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > plz help me.............plzzzzzzzzzzzz
    Worst case of literacy I've seen in a long while - I feel ill.

    Also, don't bump your own threads, especially with drivel like this.

    In the mean time, read the link in my sig, and scroll down to the literacy part.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Enabled to talk to you!!! Bilal_Rajput's Avatar
    Join Date
    Oct 2005
    Location
    .:L.A.H.O.R.E:. >>> .:P.A.K.I.ST.A.N:.
    Posts
    5
    Quote Originally Posted by Daved
    The easy way to do it is to compare the variable z with the character literal 'y' or 'n'. Remove the variables called y and n and use 'y' and 'n' in your if statements.
    thnx mate fer helping me out.......so if we have to store a character in a characater so we will have to put it in ' '
    right.....thats where i was lacking............so thnx fer ur help......

  9. #9
    Enabled to talk to you!!! Bilal_Rajput's Avatar
    Join Date
    Oct 2005
    Location
    .:L.A.H.O.R.E:. >>> .:P.A.K.I.ST.A.N:.
    Posts
    5
    Quote Originally Posted by SlyMaelstrom
    ...and this is a minor issue, but it's proper to cover both cases when using user input in an if statement.

    Try this:
    Code:
     if ( z == 'y' || z == 'Y' )
     {
         // CODE
     }
     if ( z == 'n' || z == 'N' )
     {
         // CODE
     }
    Secondly, you have no reason to use conio.h in your program. Since your not looping, the clrscr() shouldn't be doing anything at the beginning of your program and the getch() can be replaced with cin.get(), which is found in iostream.h.
    thnx mate....i ll replace getch() by cin.get()...............thnx ..

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your dot key is very sticky - better get it checked out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Enabled to talk to you!!! Bilal_Rajput's Avatar
    Join Date
    Oct 2005
    Location
    .:L.A.H.O.R.E:. >>> .:P.A.K.I.ST.A.N:.
    Posts
    5
    Quote Originally Posted by Salem
    Your dot key is very sticky - better get it checked out.
    yeah ....there is some chemistry going on between moi finger n *dot key*..hehe

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    Code:
    #include <iostream.h>
    #include <conio.h>
     int main()
     {
         clrscr();
         int grade,total=0;
         float avg;
         char z;
         
         cout << "Enter the grade of the student = ";
         cin >> grade;
         do
         {
             cout<<"\nEnter the grade of the student = ";
             cin>>grade;
             total=total+grade;
             avg=total/a;
             a++;
             cout<<"play again? y/n"<<endl;
             cin>>z; 
             }while(z == 'y' || z == 'Y'); 
         cout<<"\nAverage of the student = "<<avg<<endl;
         getch ();
         system ("PAUSE")
         return 0;
     }
    K just do something liek this i am not sure if it will work cause i never bothered debugging it but it should the as simple as that it asks over and over untill the user presses.. And it is also nice to add system pause so the screen doesn't close and you can run it from compiler........... (i liike the dots as much as u)

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by kennny2004
    And it is also nice to add system pause so the screen doesn't close and you can run it from compiler
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM