Calculating Error

This is a discussion on Calculating Error within the C++ Programming forums, part of the General Programming Boards category; This is my (Yehaa!! My first, more than 20 lines of code containing, more than 90% working) finished program. It ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Calculating Error

    This is my (Yehaa!! My first, more than 20 lines of code containing, more than 90% working) finished program.
    It is a calculator, I made it just to practise. BUT!! it doesn't work!
    Well, it does, but the outcome is 0!! No matter what I try, the outcome is 0 NULL ZERO!! How is this possible! I coun't find out why. Here is the source code, please tell me what I am doing wrong.
    Code:
    #include <iostream>     //For cout and other basic stuff
    #include <string.h>     //For char and string
    #include <stdlib.h>     //For system pause.(I don't know if including headers uses memory, but it would be a waste of space if they would cuz I don't really need this one)
    
    
    
    int main()
    
    {
    char confirm[2];
    
    while (strcmpi("q", confirm))
    
    {
    cout<<"Druk op Q en dan op enter om het programma te beeindigen, druk op enter om door te gaan.\n"; //Press Q and than enter to quit, press enter to continue.
    
    cin.getline(confirm, 2, '\n');
    
    if (!strcmpi("q", confirm))
     {
    return 0;
      }
     else
     {
     char input[2];
    
     cout<<"Wat wil je doen?? Twee cijfers: \nA: optellen.\nB: aftrekken.\nC: vermenigvuldigen\nD: delen.\n__________\n"; //What do you wish to do? A=Count two number. B=subtract two number C=Multiply two number D=Split two number. (?)
    
     cin.getline(input, 2, '\n');
    
         cout<<"__________\n";
    
     if (!strcmpi("A", input))
         {
        cout<<"Je hebt ervoor gekozen om twee getallen op te tellen\n"; //You have chosen to count two numbers.
         }
        else if (!strcmpi("B", input))
         {
         cout<<"Je hebt ervoor gekozen om twee getallen af the trekken\n"; //You have chosen to subtract two numbers.
         }
         else if (!strcmpi("C", input))
         {
         cout<<"Je hebt ervoor gekozen om twee getallen te vermenigvuldigen\n"; //You have chosen to multiply two numbers.
         }
         else if (!strcmpi("D", input))
         {
         cout<<"Je hebt ervoor gekozen om twee getallen te delen\n"; //You have chosen to split two numbers.
         }
         else
         {
         cout<<"\n!!Error, bad imput, quitting!!\n";
    
         cin.get();
    
         return 0;
         }
         system("pause");
    
         int x=0,y=0,z=0;
             {
             cout<<"Vul het eerste cijfer in: "; //Please enter the first number.
    
             cin>>x;
    
             cin.get();
    
             cout<<"\nVul nu het tweede cijfer in: "; //please enter the second number.
    
             cin>>y;
    
             cin.get();
    
             switch (input, '\n')
    
                    {
    
                    case 'A':
    
                    z=x+y;
    
                    break;
    
                    case 'B':
    
                    z=x-y;
    
                    break;
    
                    case 'C':
    
                    z=x*y;
    
                    break;
    
                    case 'D':
    
                    z=x/y;
    
                    break;
    
                    }
                    cout<<"__________\nDe uitkomst: "<<z<<"\n__________\n";     //The outcome: <<z<<; */ this is whre it goes wrong/*
    
                    }
               }
         }
    }
    The cout functions are written in dutch, but I translated them to english.

    Please use [code][/code]Tags
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Change...
    Code:
            switch (input, '\n')
    ... to...
    Code:
            switch (input[0])
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Not working

    That doesn't change anything! The outcome remains 0.
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    50
    The ONLY problem: When the program calculates the sum, the outcome is always 0.
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> That doesn't change anything!

    Yes it does. As it stands in your program, none of your case: clauses are being considered, thus it is zero, because you are never setting it to anything else.

    switch takes a single value, you are using a comma operator and thus the actual value you are testing is the last operand it sees i.e. '\n'. So your cases are saying effectively does '\n' == A, no, does '\n' == B, no does '\n' == C, no, does '\n' == D, no.

    If you don't believe me, try adding...
    Code:
    case default:
        z = 5;
        break;
    ... the default case is executed if all the others are FALSE.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    FromHolland,

    adrianX is correct, although this will still give you a zero value for "z" if you input a lowercase menu choice.

    add:

    #include <ctype.h>

    and change your switch statement to:

    switch (toupper(input[0]))

    and it runs fine. Sorry I can't give a better explaination but im very new to C++.

    BH

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    50
    Thank you VERY much !! It finally works!

    You say that you can't explain any better, well, maybe someone else can. I need to know HOW and WHY it works, that's how I learn from it.
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Letters have two cases, uppercase like A and lowercase like a. Your switch cases are saying is the input variable an 'A', i.e. an uppercase A. If you entered an a i.e. a lowercase a, then it will not match as A != a. What the toupper() function does is given a character, in whatever case, it returns the uppercase value, thus toupper('a') returns 'A' thus your switch's case 'A' now will work. There is the opposite function tolower() as well which does the opposite.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Nubi
    Guest
    Instead of using a foreign function you may want to look into the ASCII numerical codes of the different letters of the alphabet and write your own function.

    (If at all possible I try to use only my own functions/classes/templates because I can then know all of its potential and limitations. Also good for finding problems.)

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>>
    If at all possible I try to use only my own functions/classes/templates because I can then know all of its potential and limitations.
    <<<

    This approach may well be fine whilst learning or tinkering with your own projects but will not work in the professional world.

    Reuse is a key concept. You will simply not have the time to develop everything from scratch. It takes to long to do, to test, and to certify - and why would an employer pay you to do this when things like the STL are there, thourougly debugged and used by thousands of developers around the world?

    You will need to understand interfaces. Your task will most probably be to produce classes etc., that expose a specified interface and invoke methods of other classes, (not written by you), that also expose a specified interface.

    If you do not get used to this approach, you will find joining a professional software house a bit of a jolt.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Also, a custom-written touppper using the key codes you know (ASCII probably) is very limited. toupper, belonging to the CRT library, is implemented for every platform where there is a C compiler, in an efficient and working way. For example, if you tried porting your custom toupper to an IBM mainframe you would suddenly face the EBCDIC key table, which would mean that you need to reimplement the function. Using the CRT this has already been done for you.

    Or consider suddenly changing to a UNICODE environment, where there are other letters like Chinese ideograms. They don't have a concept of case AFAIK, but even other alphabets might have (Greek letters). You would have to sort out those cases too.
    Using the CRT you simply replace toupper with wtoupper which is already complete and working.

    Embrace the standard library, use it whereever you can.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    CornedBee -- I thought the truly C++ way was to define a locale-specific toupper(), based off of the locale-specific charater toupper().

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yep, right. In true C++ you shouldn't use <cctype> but rather the things from <locale>.

    So you should actually write
    std::toupper(c, std::locale());

    The advantage is that it takes heed of locale-issues and automatically supports UNICODE (if c is wchar_t, it still works).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    i don't have much to add here, just nitpicking, but:

    0 != "zero" != NULL;

    what i meant to say is that 0 and NULL are two different things...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Not in C++, or at least not on the compilers I know.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21