Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Project 1

Here are some projects of actual programs you can practice making.

Project 1

Postby antiRTFM on Tue Apr 15, 2008 7:37 pm

Hello there! Lets put our knowledge to practice and make a program!

In this project, you will be making a calculator program. A stupid one at that, but good enough to use out alot of what you learned.

Here is what your calculator program should do:

1: Ask the user to type in a number
2: Ask the user to choose one of the following 4 mathematical operators: + (addition) - (subtraction) * (multiplication) / (division)
3: Ask the user for a second number
4: Print the users math problem to the screen and add the equals (=) symbol at the end. For example:

123 / 34 =

5: Print the answer to the math problem to the screen. Print the full fractional part of the number if there might be one.
6: Ask the user to enter some stuff to exit the program.

That's all! Told you it would be stupid.

Good luck!
Last edited by antiRTFM on Tue Apr 22, 2008 1:29 am, edited 1 time in total.
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Postby Danielghofrani on Tue Apr 15, 2008 10:17 pm

Code: Select all

#include "stdafx.h"
#include <iostream>
using namespace std ;
int main ()
{

   
   char response ;
   int firstnumber ;
   int secondnumber ;
   cout << " please enter the first number " << endl ;
    cin>> firstnumber ;
   cout << " please enter which operation you wanna do "<< endl ;
   cin >> response ;
   cout << " please enter the second number " << endl ;
   cin >> secondnumber ;
   if (response == '+'){
   cout << firstnumber << " + " << secondnumber << " = " << firstnumber+secondnumber << endl ;
   };
   if (response == '-'){
   cout << firstnumber << " - " << secondnumber << " = " << firstnumber-secondnumber << endl ;
   };
   if (response == '*'){
   cout << firstnumber << " * " << secondnumber << " = " << firstnumber*secondnumber << endl ;
   };
   if (response == '/'){
   cout << firstnumber << " / " << secondnumber << " = " << firstnumber/secondnumber << endl ;
   cout << " and the remainder fot the devision would be " << firstnumber%secondnumber<< endl ;
   };
   
   char f ;
   cin >> f ;
   return 0 ;
}
Danielghofrani
 
Posts: 3
Joined: Tue Apr 15, 2008 8:54 pm

Postby antiRTFM on Wed Apr 16, 2008 2:11 am

Danielghofrani:

Excellent!- except that in the project description I said not to print the remainder, but rather
5: Print the answer to the math problem to the screen. Print the full fractional part of the number if there might be one.

Meaning that if the calculation is 4 / 5 the answer should be seen as 0.8
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Postby adrian++ on Sun May 18, 2008 2:54 pm

This should be a good calculator........

Code: Select all

#include <iostream>
#include <string>


using namespace std;

int main(){
   
    float a;
    float b;
    float c;
    int d;
    float x;
   
   string y;
   
   y = "yes";
   
   
    do{
           cout<<" I am your calculator,how may I help you\n";
   
    cout<<" 1) Add two numbers\n";
    cout<<" 2) Subtract two numbers\n";
    cout<<" 3) multiply two numbers\n";
    cout<<" 4) divide two numbers\n";
    cin>> d;

    switch(d){
              case 1:
                   cout<<"Enter two numbers to add please: \n";
                   cin>> a;
                   cin>> b;
                   
                   c = a+b;
                   
                   cout<<" The sum of the two numbers is: "<< a << " + " << b << " = " <<  c <<'\n' << endl << endl;
                   cin.get();
                   break;
             
              case 2:
                   cout<< " Enter two numbers to subtract: \n";
                   cin>> a;
                   cin>> b;
                   
                   c = a - b;
                   
                   cout<<" The difference of the two numbers is: " << a << " - " << b << " = " <<  c << '\n' << endl << endl;
                   cin.get();
                   break;
                   
              case 3:
                   cout<< " Please enter two numbers to multiply: \n";
                   cin>> a;
                   cin>> b;
                   
                   c = a * b;
                   
                   cout<<" The  sum of the numbers are: " << a << " * " << b << " = " <<  c <<'\n' << endl << endl;
                   cin.get();
                   break;
                   
              case 4:
                      cout<< " Please enter two numbers to divide: \n";
                      cin>> a;
                      cin>> b;
                     
                      c = a / b;
                     
                      cout<< " The answer is: "<< a << " /" << b << " = " << c << '\n'<< endl << endl;
                      cin.get();
                      break;
                     
              default:
                      cout<< "Error, you've made a mistake \n" << endl << endl;
                      break;
                      }
                      cout << " Do you want to continue using the calcultor ?  if you do type yes, if not type no. \n \n \n" ;
                      cin >> y;
                     
                     
                        cin.get();
} while( y == "yes");
                       
                       }


adrian++
 
Posts: 11
Joined: Sun May 18, 2008 2:15 pm

Postby antiRTFM on Sun May 18, 2008 8:06 pm

adrian++

Excellent job!
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Postby Iacoopa on Sat May 24, 2008 2:59 pm

Here is my calc. I would've used a switch statement but strings are not allowed in them (according to the errors I got when I tried to compile with a string switch statement)

so Here it is:
Code: Select all
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
int main();

int main()
{
   float a;
   float b;
   string choice;
   string cont;

   cout <<"Welcome, how may I help you today?\nAdd\nSubtract\nDivide\nMulitply\n";
   cin >> choice;


   if (choice == "add")
   {
       cout<<"Choose your first number: ";
       cin >> a;
       cout<<"\nChoose your second number: ";
       cin >> b;
       cout << a <<" + " << b <<" = " <<a+b <<endl;
       cout <<"continue? yes or no: ";
       cin >> cont;
         if (cont == "yes")
         {
            main();
         }
         else
         {
            return 0;
         }
   }

   if (choice == "multiply")
   {
       cout<<"Choose your first number: ";
       cin >> a;
       cout<<"\nChoose your second number: ";
       cin >> b;
       cout << a <<" x " << b <<" = " <<a*b <<endl;
       cout <<"continue? yes or no: ";
       cin >> cont;
         if (cont == "yes")
         {
            main();
         }
         else
         {
            return 0;
         }
   }

   if (choice == "divide")
   {
       cout<<"Choose your first number: ";
       cin >> a;
       cout<<"\nChoose your second number: ";
       cin >> b;
       cout << a <<" / " << b <<" = " <<a/b <<endl;
       cout <<"continue? yes or no: ";
       cin >> cont;
         if (cont == "yes")
         {
            main();
         }
         else
         {
            return 0;
         }
   }

   if (choice == "subtract")
   {
       cout<<"Choose your first number: ";
       cin >> a;
       cout<<"\nChoose your second number: ";
       cin >> b;
       cout << a <<" - " << b <<" = " <<a-b <<endl;
       cout <<"continue? yes or no: ";
       cin >> cont;
         if (cont == "yes")
         {
            main();
         }
         else
         {
            return 0;
         }
   }
   
   string exit;
   exit = exit;
   cin >> exit;
   return 0;
}


It was very easy because after the first IF all I had to do was copy-paste and change the facotrs in the if statement and the mathematical process :D
Iacoopa
 
Posts: 7
Joined: Thu May 22, 2008 7:18 pm
Location: Mars

Postby antiRTFM on Sat May 24, 2008 10:06 pm

Great job!
Iacoopa wrote:...I would've used a switch statement but strings are not allowed in them (according to the errors I got when I tried to compile with a string switch statement)

Yummy... I like viewers who test and discover things out :)

Stay tuned for video # "wrap 6 @ video 40" where I mention something about calling functions inside of the function itself (like when you call main() inside of main() )

p.s. what are you trying to acheive with "exit = exit;" ?
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Postby Iacoopa on Sun May 25, 2008 1:51 pm

I dont know, I guess I wasn't thinking o.0 Glad you like it anyways! :D
Iacoopa
 
Posts: 7
Joined: Thu May 22, 2008 7:18 pm
Location: Mars

My Attempt at this program.

Postby leebrook on Wed May 28, 2008 11:58 am

Hi,

This is my attempt at this test, I've only being doing programming for 4 days so please excuse any obvious mistakes.


Code: Select all

#include <iostream>

using namespace std;

int main ()

{

   int number;
   
   int number2;
   
   char choice;
   
   cout << "\n";
   
   cout << "Hello welcome to our simple calculator, lets do some maths!\n";
   
   cout << "\n";
   
   cout << "Please enter your first number:";
   
   cin >> number;
   
   cout << "\n";
   
   cout << "Good Choice, Now decide if you would like to add +, subtract -, times *, or divide /:";
   
   cin >> choice;
   
   cout << "\n";
   
   cout << "Great, now pick your second number:";
   
   cin >> number2;
   
   cout << "\n";
   
   cout << "Thanks, Ok here is your problem:";
   
   cout << number << choice << number2;
   
   cout << "=\n";
   
   cout << "\n";
   
   cout << "and here is your answer:";
   
   if (choice == '+') cout << (number + number2);
   
   else if (choice == '-') cout << (number - number2);
   
   else if (choice == '*') cout  << (number * number2);
   
   else if (choice == '/') cout << (number / number2);
   
   cout << "\n";
   
   cout << "\n";
   
   cout << "Thanks for using our simple calculator, Goodbye!\n";
   
   char f;
   
   cin >> f;
   
   return 0;
   
}   
   
   
leebrook
 
Posts: 1
Joined: Wed May 28, 2008 11:10 am

Postby `Sp3ke on Fri Jun 06, 2008 9:48 am

Here's mine :

Code: Select all
//Project #1 - Calculator

#include <iostream>
#include <string>

using namespace std;

int main()

{
   
    int f;
    double FirstNumber, SecondNumber;
    string Operator;
    string quit;
   
    cout<<"Enter your first number"<<endl;
    cin>>FirstNumber;
    cout<<"Enter your second number"<<endl;
    cin>>SecondNumber;
    cout<<"Imput the operator you wish to use (* / + -)"<<endl;
    cin>>Operator;
   
    if(Operator == "*"){
                cout<<FirstNumber<<"*"<<SecondNumber<<"="<<FirstNumber * SecondNumber <<endl;
               
                cout<<"Do you wish to quit or use again? (Quit / Use)"<<endl;
                cin>>quit;
                if(quit == "Quit"){
                        return 0;
                        }
                        else{
                             main();
                             }           
                }
               
               
               
                else if(Operator == "/"){
                     cout<<FirstNumber<<"/"<<SecondNumber<<"="<<FirstNumber / SecondNumber <<endl;
                     
                     cout<<"Do you wish to quit or use again? (Quit / Use)"<<endl;
                cin>>quit;
                if(quit == "Quit"){
                        return 0;
                        }
                        else{
                             main();
                             }   
                     
                     }
                     
                     else if(Operator == "+"){
                          cout<<FirstNumber<<"+"<<SecondNumber<<"="<<FirstNumber + SecondNumber <<endl;
                         
                          cout<<"Do you wish to quit or use again? (Quit / Use)"<<endl;
                cin>>quit;
                if(quit == "Quit"){
                        return 0;
                        }
                        else{
                             main();
                             }   
                         
                          }
                         
                          else if(Operator == "-"){
                               cout<<FirstNumber<<"-"<<SecondNumber<<"="<<FirstNumber - SecondNumber <<endl;
                               
                               cout<<"Do you wish to quit or use again? (Quit / Use)"<<endl;
                cin>>quit;
                if(quit == "Quit"){
                        return 0;
                        }
                        else{
                             main();
                             }   
                               
                               }
   
   
   
    cin>>f;
   
}


`Sp3ke
 
Posts: 12
Joined: Sun May 25, 2008 10:09 am

Postby Mythik on Sat Jun 07, 2008 9:14 am

Hi!,

I made one too, here's my code :

Code: Select all
#include <iostream>
using namespace std;

int main()
{
    string cont;
    do
    {
     cout << "Welcome to Calculator!\n";
     double x,y,z;
     string oP;
         
     cout << "Please enter the first number : ";
     cin >> x;
     cout << "What operator do you wish to use (* , / , + or -) : ";
     cin >> oP;
     cout << "Please enter the second number : ";
     cin >> y;

     if (oP=="+")
          z=x+y    ;
     else if (oP=="-")
          z=x-y    ;
     else if (oP=="*")
          z=x*y    ;
     else if (oP=="/")
          z=x/y    ;
     
     cout << "\nThe answer to " << x << " " << oP  << " " << y << " is " << z;
     cout << "\n\nEnter 'r' to restart, anything else will quit the program : ";
     cin  >> cont; cout << endl;
    }
    while (cont=="r");
   
    return 0;


Greetings to you all!
Mythik 8)

(ps. I changed a few of the lines in my code a few hours after posting my original code. Because nobody already reacted on this post I tought it would be better to change the code in this post in stead of making a new post with the updated code).
Last edited by Mythik on Sat Jun 07, 2008 4:21 pm, edited 2 times in total.
User avatar
Mythik
 
Posts: 8
Joined: Thu Jun 05, 2008 6:55 pm

Postby Mythik on Sat Jun 07, 2008 1:03 pm

Hmm, changed my code again. I'll keep it the way it is from now on 8).

(Changed it one more time. really made my last change now! :wink: )
User avatar
Mythik
 
Posts: 8
Joined: Thu Jun 05, 2008 6:55 pm

My Calculator

Postby mtg1981 on Wed Jul 02, 2008 9:52 pm

Hi, I am fairly new to C++, and it is my first attempted language. So if my code is sloppy, I am sorry!

Code: Select all
// Edit V1.1
#include <iostream>
#include <string>
using namespace std;


int main()
{
    double dFirst;
    double dSecond;
    double dSum;
    char cSymbol;

system("cls");

cout << "Welcome to my test calculator program!\n";
cout <<  "Please enter your first number: \n";
       
cin >> dFirst;

cout << "how would you like the number to be handled? \n";
cout << "+ for Addition, - for Subtraction, * for Multiplication,\n";
cout << "/ for Division: \n";
       
cin >> cSymbol;

cout << "\nNow Please enter your second number: \n";

cin >> dSecond;

if (cSymbol == '/' && dFirst >= dSecond)
   {
        dSum = (dFirst / dSecond);
        cout << dFirst << " divided by " << dSecond << "=" << dSum << endl;
   } else
          if (dFirst < dSecond && cSymbol == '/')
          {
              cout << "\nNow you know that wont work.\n \a";
              }
if (cSymbol == '-' && dFirst >= dSecond)
   {
        dSum = (dFirst - dSecond);
        cout << dFirst << " minus " << dSecond << "=" << dSum << endl;
   } else
       if (dFirst < dSecond && cSymbol == '-')
          {
              cout << "\nNow you know that wont work.\n \a";
              }
if (cSymbol == '+')
    {
         dSum = (dFirst + dSecond);
         cout << dFirst << " plus " << dSecond << "=" << dSum << endl;
         }
if (cSymbol == '*' || cSymbol == 'x' || cSymbol == 'X')
    {
         dSum = (dFirst * dSecond);
         cout << dFirst << " times " << dSecond << "=" << dSum << endl;
         }
cout << "\nType yes to continue, or anything else to quit.";         
string sFinished;
cin >> sFinished;
if (sFinished == "yes" || sFinished == "Yes" || sFinished == "YES")
    main();
   
return 0;
}
mtg1981
 
Posts: 1
Joined: Wed Jul 02, 2008 9:34 pm

Postby torment on Sun Jul 13, 2008 8:07 pm

Well this is my calculator
im new to c++ i have only done it for like 1 week but here it goes.


Code: Select all
#include <iostream>
using namespace std;
int main()
{
    float a;
    char b;
    char c = '+';
    float d;
    float e;
    float f;
    cout << "enter a number: ";
    cin >> a;
    cout << "whould you like to + - / or * the number? ";
    cin >> b;
        if (b == '/')
    {
          cout << "Enter a secound number: ";
          cin >> d;
          cout << a/d;
          system("Pause>nul");
          return 0;
    }
        if (b == '*')
    {
          cout << "Enter a secound number: ";
          cin >> d;
          cout << a*d;
          system("Pause>nul");
          return 0;
    }   
    if (b == '+')
    {
          cout << "Enter a secound number: ";
          cin >> d;
          cout << a+d;
          system("Pause>nul");
          return 0;
    }
    if (b == '-')
    {
          cout << "Enter a secound number: ";
          cin >> d;
          cout << a-d;
          system("Pause>nul");
          return 0;
    {


system("Pause>nul");
return 0;
}}}
torment
 
Posts: 2
Joined: Sun Jul 13, 2008 8:02 pm

Postby torment on Mon Jul 14, 2008 9:20 am

Oops wrong code heres the real code

Code: Select all
#include <iostream>
using namespace std;
int main()
{
    float a;
    char b;
    char c;
    cout << "enter a number: ";
    cin >> a;
    cout << "whould you like to + - / or * the number? ";
    cin >> b;
        if (b == '/')
    {
          cout << "Enter a secound number: ";
          cin >> d;
          cout << a/d;
          system("Pause>nul");
          return 0;
    }
        if (b == '*')
    {
          cout << "Enter a secound number: ";
          cin >> d;
          cout << a*d;
          system("Pause>nul");
          return 0;
    }   
    if (b == '+')
    {
          cout << "Enter a secound number: ";
          cin >> d;
          cout << a+d;
          system("Pause>nul");
          return 0;
    }
    if (b == '-')
    {
          cout << "Enter a secound number: ";
          cin >> d;
          cout << a-d;
          system("Pause>nul");
          return 0;
    {


system("Pause>nul");
return 0;
}}}
torment
 
Posts: 2
Joined: Sun Jul 13, 2008 8:02 pm

Next

Return to Projects

Who is online

Users browsing this forum: No registered users and 0 guests

cron