Teen Programmers Unite  
 

 

Return to forum top

C++ Help Required

Posted by andymcnab [send private reply] at July 23, 2002, 05:32:18 AM

Hello,
Here is the code for a small begginers (extremely simple program). This is all i can really write and was wondering if any of you guys have any next steps for me in learning c/c++. I know i definitly want to learn c++ ultimately. p.s Does any1 know of any sites that have a tutorial that takes you through making some simple programs cos i learn very well from doing rather than reading. Thanx. Oh yeah i have read a couple of c++ books but it gets to a certain point and i get lost! (Kernighan and Ritchie: The C++ Programming Language). Here is the program then:

#include <stdio.h>
#include <iostream.h>

int main(void)
{
float no1, no2, answer;
int type;

cout<<"Please enter number 1: ";
cin>>no1;
cout<<"Please enter number 2: ";
cin>>no2;
cout<<"1 = Divide (/)\n"<<"2 = Mulitply (*)\n"<<"3 = Subtract(-)\n"<<"4 = Add(+)\n";
cin>>type;
if(type == 1)
{
answer = no1 / no2;
cout<<no1<<" / "<<no2<<" = "<<answer<<"\n";
return 0;
}
if(type == 2)
{
answer = no1 * no2;
cout<<no1<<" * "<<no2<<" = "<<answer<<"\n";
return 0;
}
if(type == 3)
{
answer = no1 - no2;
cout<<no1<<" - "<<no2<<" = "<<answer<<"\n";
return 0;
}
if(type == 4)
{
answer = no1 + no2;
cout<<no1<<" + "<<no2<<" = "<<answer<<"\n";
}
else
{
cout<<"Incorrect input, Quitting!";
return 0;
}
}

This is very simple but its all i have the knowledge to write, i would like to write some internet apps, i prefer to use the win32 console apps rather than the MFC which i think is pretty shitty when VB does such a better job but idotn want to spark a discussion on which is better :-). Pls give suggestions of sites and program source to download to study and help me!. Thanx all...

Posted by diegoeskryptic [send private reply] at July 23, 2002, 11:45:06 AM

Hey andy... if you go to the C++ learning group.... look at sites that are listed there... I recommended the last site that is listed there its from www.pragsoft.com.... another good site is www.Gametutorials.com... in the tutorial section.... good luck

Posted by unknown_lamer [send private reply] at July 23, 2002, 11:50:11 AM

Why are you using if..then..else's there? Case would be a better choice...and K&R have nothing to do with C++.

Posted by Zandalf [send private reply] at July 25, 2002, 01:42:49 AM

I know this isn't what you asked for, but I'm going to edit the code above because I find that seeing better ways to do things helps you to understand the shortcuts you can use later on ^_^

#include <iostream>    // not using anything from stdio, so don't.
// also, don't include iostream.h, because that is the old STL, and wasn't
// standardized, plus it's two more charcters to type ^_^

using namespace std;     // this is the gimpy way, but it's easiest

int main(void)
{
    float n1, n2;    // you don't need an answer variable
    int oper;

    cout << "Please input the first number\n:";
    cin >> n1;
    cout << "Please input the second number\n:";
    cin >> n2;
    cout << "Please choose the operation (1 = div, 2 = mult, 3 = sub, 4 = add)\n:";
    cin >> oper;

    switch(oper)
    {
    case 1: // div
        cout << n1 << " \ " << n2 << " = " << n1/n1 << endl;
        break;
    case 2: // mult
        cout << n1 << " * " << n2 << " = " << n1*n1 << endl;
        break;
    case 3: // sub
        cout << n1 << " - " << n2 << " = " << n1-n1 << endl;
        break;
    case 4: // add
        cout << n1 << " + " << n2 << " = " << n1+n1 << endl;
        break;
    default:
        cout << "invalid operator code\n";
    }

    return 0;
}
Posted by unknown_lamer [send private reply] at July 25, 2002, 09:36:34 AM

What is so hard about typing std:: in front of all of the STL stuff? It isn't that hard; I do it every day!

Posted by andymcnab [send private reply] at July 26, 2002, 03:00:11 AM

Thanx for the updated code i now know and understand Switch Case's Arguments. Thanx all who posted!

Posted by Zandalf [send private reply] at July 26, 2002, 06:23:25 PM

unknown - because I am the epitome of the lazy programmer, and if I don't have to type std:: in front of every command, I'm a happy lazy programmer ^_^ besides, it's not like I'm going to be making any vars called cout or cin or vector of map, because these terms are so generalized that they deserve to be generalized programming terms (i.e. STL).

Posted by tastytrout [send private reply] at July 28, 2002, 05:04:49 PM

Get "The C++ Programming Language" by Bjarne Stroustrup (a.k.a., the c++ bible). It written by the guy who developed C++.

Posted by buzgub [send private reply] at July 29, 2002, 12:54:40 AM

I've been led to believe that "The C++ Programming Language" by Bjarne Stroustrup is heavy reading, with some language implementation details and the like that raise the difficulty level somewhat.

Posted by unknown_lamer [send private reply] at July 29, 2002, 11:12:54 AM

I found it easy to read.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.