Beginning Tutorials

This is a discussion on Beginning Tutorials within the C++ Programming forums, part of the General Programming Boards category; My first post. I am trying to learn C++ and have been reading the tutorials (learn something! Presidential order…seams reasonable). ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    Beginning Tutorials

    My first post.

    I am trying to learn C++ and have been reading the tutorials (learn something! Presidential order…seams reasonable). Books are on the way. Anyway, as an exercise, I’ve taken some of the tools presented here and put them into one thing.


    Code:
    #include <iostream>
    
    using namespace std;
    int a;
    void playgame()
    {
       cout << "Play";
    }
    
    
    
    int main()
    {
    for (;a=1;)
    {
     int input;
     cout<<"1. Multiply\n";
     cout<<"2. Devide\n";
     cout<<"3. Add\n";
     cout<<"4. Exit\n";
     cout<<"Selection: ";
     cin>> input;
     switch (input)
     {
     case 1:
       playgame();
       cout<<" Multiplication\n";
       {float mult ( int x, int y );
       float x;
       float y;
       cout<<"Please input two numbers to be multiplied: ";
       cin>> x >> y;
       cin.ignore();
       cout<<"The product of your two numbers is "<< x * y <<"\n";
       a=1;
       }
       break;
     case 2:
       playgame();
       cout<<" Devision\n";
       {float div (int x, int y);
       float x;
       float y;
       cout<<"Please enter a number for a dividend and a number for a devisor: ";
       cin>> x >> y;
       cin.ignore();
       cout<<"The quotent is "<< x / y <<"\n";
       a=1;
       }
       break;
     case 3:
       playgame();
       cout<<" Addition\n";
       {float add (int x, int y);
       int x;
       int y;
       cout<<"Please enter two numbers to add: ";
       cin>> x >>y;
       cin.ignore();
       cout<<"The result is "<< x + y <<"\n";
       a=1;
       }
       break;
     case 4:
       cout<<"Thank you for playing!\n";
       break;
       a=0;
     default:
       a=0;
       cout<<"Error, bad input, quitting\n";
       break;
       }
       cin.get();}
    cout<< a <<endl;
    }
    Ok, it kinna sorta does what I expected. BUT! I expected it would quite automatically if case 4 were in play. It doesn’t. It just continues to loop. Same with default.
    Any hint about what I’m missing? I’ve tried sooooo many things, too many to list here.

    Thanks,
    Sug
    Last edited by Salem; 12-28-2011 at 06:46 AM. Reason: removed most of the formatting disaster

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    case 4:
       cout<<"Thank you for playing!\n";
       break;  // I think you know what "break" is supposed to do.
       a=0;    // so perhaps this is in the wrong place? ;)
    I expected it would quite automatically if case 4 were in play. It doesn’t. It just continues to loop.
    Well, here's the loop condition:
    Code:
    for (;a=1;)
    When I started programming seriously I had to write a giant:
    ==
    at the top of my keyboard because I made this mistake so often and couldn't see it. That's the equality compare operator. "=" is the assignment operator.

    Keep in mind that even if you correct that, you still have the problem I first mentioned.
    Last edited by MK27; 12-28-2011 at 06:00 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Your indentation is horrible. This is a high priority item to fix.
    Also, these
    float mult ( int x, int y );
    make no sense. They are function declarations, but you have no such functions in your code.
    Even if you were to define a function, you can't define them within another function (they must be outside other functions), plus, you never really call them. So review functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Quote Originally Posted by Elysia View Post
    Your indentation is horrible. This is a high priority item to fix.
    Seen much worse around here.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Hey! Fixed it! Although it was not something that ever had to be fixed really. I’m not getting paid or graded for this. Nevertheless it is very cool to see it do what I expect it to do. While I am happy about that I realize there is a looooooong way to go. Sure it does what I expect but I do not know why and until I do know why I don’t know jack.

    MK27 you were spot on. I did 2 things.

    Code:
    int main()
    {
    a=1;
    for (;a==1;)
    And
    Code:
    case 4:
    a=0;
    cout<<"Thank you for playing!\n";
    break;
    Elysia, I looked up the indent thing. I’ll work on it.

    There is one thing though…If
    float mult ( int x, int y ); makes no sense then why does it work? If I write int mult (intx, inty); and the compile and try to multiply say 3.14159 times 5.5 the result is a computerized hissy fit. But float mult (intx, inty); yields an acceptable result. Therefor it makes some sense, maybe not all the sense available but some portion of sense. That said I will keep all that everyone has put forth here in mind as I push on.


    Thanks to all!
    ~S~!
    Sug

    Obviously I still don't know how to post code, sorry all.

    Last edited by Sugar; 12-28-2011 at 08:41 PM.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Quote Originally Posted by Sugar View Post
    There is one thing though…If [/SIZE][/FONT][COLOR=black][FONT=Verdana]float mult ( int x, int y ); makes no sense then why does it work? If I write int mult (intx, inty); and the compile and try to multiply say 3.14159 times 5.5 the result is a computerized hissy fit. But float mult (intx, inty); yields an acceptable result. Therefor it makes some sense, maybe not all the sense available but some portion of sense. That said I will keep all that everyone has put forth here in mind as I push on.
    Ah, post hoc, ergo propter hoc.

    The statement
    float mult ( int x, int y );
    is a function declaration, also known as a function prototype. There is no restriction in the placement of such statements. You can in fact declare functions anywhere, but you shouldn't want to do it exactly like that. Be organized; declare functions in one place at the top of a file. There usually aren't any problems as far as compiling is concerned until you actually call the function. There has to be a definition, somewhere, that explains what mult does.

    Code:
    float mult ( int x, int y )
    {
       return x * y;
    }
    
    // call in client code:
    float prod = mult( arg, arg2 );
    Like that. Since you didn't actually call mult there was no obvious problem.

    To additionally clarify things, I will say that you opted to compute the results inline like this.
    Code:
       cout<<"The product of your two numbers is "<< x * y <<"\n";
    So that protected you from problems.

    Basically, my advice is if you want to write functions, please also try to use them. The compiler is very good at tweaking you about errors with functions and their calls. Also please don't focus on making your posts look fancy anymore.
    Last edited by whiteflags; 12-28-2011 at 09:07 PM.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Now that is just freaking farout useful.

    ~S~

    Sug

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    "Ah, post hoc, ergo propter hoc."

    Looked it up. More to learn here than meets the eye.

    ~S~!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginning ASM.
    By Krak in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2004, 05:27 PM
  2. Help with beginning program
    By CryptiniteDemon in forum C++ Programming
    Replies: 8
    Last Post: 07-17-2004, 08:37 PM
  3. Replies: 3
    Last Post: 07-11-2003, 04:22 PM
  4. beginning C++
    By datainjector in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2002, 01:27 AM
  5. In the beginning...
    By CAP in forum Game Programming
    Replies: 21
    Last Post: 05-29-2002, 01:40 PM

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