Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 N00B Questions.
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
0x8000




PostPosted: Thu Jun 15, 2006 7:37 pm   Post subject: N00B Questions.

Hey, I just started to program today, I got bored and started reading a dumb book I found in my basement for learning C++. I started experimenting with it and it's not too hard, I download Dev C++ and am using it to compile my programs. So far I've learned only a bit, but I've only been studieing it for about a little over an hour. Anyway I just decided to make my own simple math program, take any two numbers and add, subtract, multiple, or divid them. I have it working but I don't know what the code is to keep the command window open. I'd like to have it close by maybe pressing a hotkey, like Esc or Del...

Heres what I got:
code:

#include <iostream.h>
int main()
{
    int subject, num1, num2, answr;
   
    cout << "DO MATH WITH SIMPLE PROGRAM!\n";
    cout << "Multiply, Add, Subtract, and Divide!\n";
    enum math { mul=1, add, sub, div };
    math dos;
   
    cout << "\nDo you want to multiply(1), add(2), subtract(3), or divid(4)? ";
    cin >> subject;
   
    dos = math(subject);
   
    if (dos == mul) // Checks if 1 was selected for Muliplication
    {
    cout << "\nMULTIPLICATION!";
    cout << "\nEnter numberator: ";
    cin >> num1;
    cout << "\nEnter denomenator: ";
    cin >> num2;
    answr = num1 * num2;
    cout << "\n";
    cout << num1;
    cout << " X ";
    cout << num2;
    cout << " = ";
    cout << answr;
}
    if (dos == add) // Checks if 2 was selected for Addition
    {
    cout << "\nADDITION!";
    cout << "\nEnter numberator: ";
    cin >> num1;
    cout << "\nEnter denomenator: ";
    cin >> num2;
    answr = num1 + num2;
    cout << "\n";
    cout << num1;
    cout << " + ";
    cout << num2;
    cout << " = ";
    cout << answr;
}
    if (dos == sub) // Checks if 3 was selected for Subtraction
    {
    cout << "\nSUBTRACTION!";
    cout << "\nEnter numberator: ";
    cin >> num1;
    cout << "\nEnter denomenator: ";
    cin >> num2;
    answr = num1 - num2;
    cout << "\n";
    cout << num1;
    cout << " - ";
    cout << num2;
    cout << " = ";
    cout << answr;
}
    if (dos == div) // Checks if 4 was selected for Division
    {
    cout << "\nDIVISION!";
    cout << "\nEnter numberator: ";
    cin >> num1;
    cout << "\nEnter denomenator: ";
    cin >> num2;
    answr = num1 / num2;
    cout << "\n";
    cout << num1;
    cout << " / ";
    cout << num2;
    cout << " = ";
    cout << answr;
}
    else // If something other than 1-4 was selected this statement is made
        cout << "You didn't select from the categories... idiot!";
    return 0;
}

So if I can get help answering that question, and if anyone has any pointers whatsoever.. thanks.[/code]
Sponsor
Sponsor
Sponsor
sponsor
Martin




PostPosted: Thu Jun 15, 2006 8:57 pm   Post subject: (No subject)

To make it stop, #include<cstdlib> and put the line getch(); at the bottom of your code (on the line above the return 0; ) to make it wait for a keypress.

code:
cout << "something";
cout << "something else";
cout << "\n";


Can be simplified to:
code:
cout << "something" << "something else" << endl;


You should use endl instead of "\n" so that if you compile your code on a system where '\n' isn't the endline character, it will still end the line (endl is set to whatever the endline character is on the current system).

Instead of #include<iostream.h>, the standard is to use #include<iostream>

This puts cout inside the 'namespace' called std (short for standard). A namespace is a collection of functions. They use them because C++'s library is so big, there are going to be different functions with the same name. So, instead of saying cout, you have to specify which cout you want to use. You do this by saying 'I want to use the standard (std's) cout', as follows:
code:
std::cout << "Hey, this is the standard cout!" << std::endl;

That said, you can also tell the compiler that, whenever you don't specify which cout you're using, assume that you mean the standard one. To do this, give it the line:
code:
using namespace std;

below the include statements. The following two programs are equivalent:
code:
#include<iostream>
int main( )
{
  std::cout << "Hello, World" << std::endl;
  return 0;
}

code:
#include<iostream>
using namespace std;
int main( )
{
  cout << "Hello, World" << endl;
  return 0;
}



You could also put the entire program in a loop, allowing the user to check multiple things. Play with this code:
code:
#include<iostream>
using namespace std;
int main( )
{
  int input = 1;
  while( input != 0 )
  {
    cout << "Give me a number, or 0 to quit." << endl;
    cin >> input;
    cout << "Cool! You picked " << input << "!" << endl;
  }
  cout << "Sayonara!" << endl;
  return 0;
}


In your if statement, you're making too many checks.

code:
if( number == 1 )
{
  ...
}
if( number == 2 ) //Even if number was equal to 1, we're checking if it's 2!
{
  ...
}
if( number == 3 )
...


Instead of multiple if statements, use an if - else if - else structure.

code:
if( number == 1 )
{
  ...
}
else if( number == 2 ) //only checked if number isn't 1
{
 ...
}
else if( number == 3 ) //only checked if number isn't 1 or 2
{

}
else //if all else fails, do this. (it's perfectly valid to not include this line)
{

}


Finally, for if statements, you can also use a switch/case structure. I'll leave this as an exercise for you to figure out.

Good job using enums by the way!
0x8000




PostPosted: Thu Jun 15, 2006 9:49 pm   Post subject: (No subject)

Thanks a bunch Martin. Great information that I will implement into my little program. I'm really liking this programming thing, it's not as hard as I thought it would be, but then again I'm not programming anything complicated. Only a simple math program. Anyways thanks again, I'll be sure to hang around the forums, seems like there are lots of intelligent people here. Got work tomorrow, it's almost eleven, so I'll be heading off now. Smile
Martin




PostPosted: Thu Jun 15, 2006 10:52 pm   Post subject: (No subject)

Anytime!

One of the best things that you can do when learning a new programming language (or anything at all really) is to take something simple and polish it to perfection.
0x8000




PostPosted: Fri Jun 16, 2006 9:18 pm   Post subject: (No subject)

I fixed it all up, is there anything else I can add/remove or change?
code:

/*
  Name: 0x8000
  Author: 0x8000
  Date: 15/06/06 23:20
  Description: Simple math program to add/subtract/divide/multiply any two numbers
*/

#include <iostream.h>
using namespace std;
int main()
{
    int subject, num1, num2, answr;
   
    std::cout << "DO MATH WITH SIMPLE PROGRAM!\n" << "Multiply, Add, Subtract, and Divide!" << std::endl;
    enum math { mul=1, add, sub, div };
    math dos;
   
    while( subject != 0 )
    {
    std::cout << "\nDo you want to multiply(1), add(2), subtract(3), divide(4), or exit(0)? ";
    cin >> subject;
   
    dos = math(subject);
   
    if (dos == mul) // Checks if 1 was selected for Muliplication
    {
    std::cout << "\nMULTIPLICATION!" << "\nEnter numberator: " << std::endl;
    cin >> num1;
    std::cout << "\nEnter denomenator: " << std::endl;
    cin >> num2;
    answr = num1 * num2 ;
    std::cout << "\n" << num1 << " X " << num2 << " = " << answr << std::endl;
}
    else if (dos == add) // Checks if 2 was selected for Addition
    {
    std::cout << "\nADDITION!" << "\nEnter numberator: " << std::endl;
    cin >> num1;
    std::cout << "\nEnter denomenator: " << std::endl;
    cin >> num2;
    answr = num1 + num2;
    std::cout << "\n" << num1 << " + " << num2 << " = " << answr << std::endl;
}
    else if (dos == sub) // Checks if 3 was selected for Subtraction
    {
    std::cout << "\nSUBTRACTION!" << "\nEnter numberator: " << std::endl;
    cin >> num1;
    std::cout << "\nEnter denomenator: " << std::endl;
    cin >> num2;
    answr = num1 - num2;
    std::cout << "\n" << num1 << " - " << num2 << " = " << answr << std::endl;
}
    else if (dos == div) // Checks if 4 was selected for Division
    {
    std::cout << "\nDIVISION!" << "\nEnter numberator: " << std::endl;
    cin >> num1;
    std::cout << "\nEnter denomenator: " << std::endl;
    cin >> num2;
    answr = num1 / num2;
    std::cout << "\n" << num1 << " / " << num2 << " = " << answr << std::endl;
}
    else // If something other than 0-4 was selected this statement is made
        std::cout << "Please select a number from the given categories. (1-4)" << std::endl;
}
std::cout << "Exiting program..."; //If 0 is selected the program will exit
return 0;
}

I did notice that if I were to say chose division and then give a number like 4444444444, it would make the program go completely crazy repeating the text of the quesiton over and over forever...
wtd




PostPosted: Fri Jun 16, 2006 9:59 pm   Post subject: (No subject)

"iostream.h"...

Check Martin's post again. Wink
wtd




PostPosted: Fri Jun 16, 2006 10:07 pm   Post subject: (No subject)

Your indenting could also use work.

A logic error:

Your while loop tests to see if subject != 0.

The problem is that subject has not at that point been initialized. At this point it contains some random value. You may either wish to have subject initialized, or use a do while loop to ensure that that the loop runs at least once.
Martin




PostPosted: Fri Jun 16, 2006 11:23 pm   Post subject: (No subject)

A note on indentation:

My personal preference, and that of a lot of C++ programmers is to follow the simple style that anything in the braces { } gets indented a number of spaces.

For example

code:

...
//no indentation for functions
int main()
{
  //everything within main()'s two braces gets indented 2 spaces
  int x = 4;
  //the declaration of a while loop stays at whatever indentation everything else is at
  while( x < 10 )
  {
    //Now, we're inside the while loop's braces. Everything inside here gets an additional two spaces, for a total of four
    x += 1;
    if( x == 6 )
    {
      //And now we're at 6, and so on..
      cout << "X is 6!" << endl;
    }
  }
  //back down to two spaces
  return 0;
}


#include<iostream.h> versus #include<iostream> : iostream.h is the old version, iostream is the new one. The new one is nicer, you should use that.

using namespace std;

Your code is syntactically fine, but there is redundency in it. The using namespace std; line is telling C++ that 'If you see a function and you don't know what it is, it's coming from the standard namespace - namespace std' So your code comes up to a lonely cout function, says 'wait a sec, he told me that it'd be in the standard namespace, which is defined by iostream.' It checks there, and you're off.

So you're telling it to check std as a default, and then you're specifically checking std when coding. So, in summary - either get rid of the using namespace std; line, or get rid of the std::'s. I'd go with the latter.

A thing to note: cin is also part of the standard namespace. If you were to get rid of the using namespace std; line, you'd need to say std::cin instead of just cin.

For now though, I just wouldn't worry about it. Keep the using namespace std; line in, forget about the std:: things and carry on. Namespaces are something that you'll come to explore in depth later in learning, and it really won't make a ton of sense until then anyway. So much fuss over a single line of code eh?
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: