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.

Re: Project 1

Postby noobgrammer on Fri Sep 25, 2009 1:15 pm

Ya I'm pretty sure I know what I'm talking about and yes your program will work just as it is

do this and you will see how not using the else if ladder for one is time saving
two it could keep you out of trouble not using else ifs would be like not putting breaks in a switch statement

Put a break point on your first if in calculator2() function and run it in debug

if you don't know how it's good to know how so I'll say how to do it just in case

to put a break point in click to the left of the line numbers (if you don't have line numbers showing)
the far left of the code window. You should see a red dot that's your break point now press F5 (not Ctr F5) do the calculator and do the 3 number then say add on both and after you do that you will see the program stops make sure you in visual studio and push the F10 that will walk you through how the compiler reads the program and you will see the after it reads the if statement that's true it stills checks the other ifs which is pointless and could cause headaches if future programming. Now after you seen how it goes and checks all the ifs go ahead and press shift F5 to stop debugging now go and add a else before all the ifs but the first and run the debug again and watch what it does this time.

I only told you to use the else if ladder because that program would run better (not that you would notice)but to help give you a idea of what there is and what you can do. There will be times when a ladder of just ifs are necessary though. As you write programs you will see.

Using the debug can be very very helpful it shows you how the compiler is running your code.
I know I've had problems where it had different ideas on what to do then what I had in mine and I would have to run it to see what I did wrong.
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Project 1

Postby cawwum on Fri Sep 25, 2009 6:58 pm

Im not saying for a second that you dont know what you are talking about.. I mean I am the noob and you are the one that has been doing this for a while.. (I guess)

I was just saying that you can do it my way because you were saying it as if it wouldnt work.. I understand that the else is very important in alot of cases..
When I tried doing a if/else ladder on my first attempt at the calculator it came up with a error and when I took the elses out it worked fine, I know that it was just me making a mistake but what I mean is that, that is the reason that I didnt put the else in the code...
oh well thanks for the advice =]
cawwum
 
Posts: 5
Joined: Thu Sep 17, 2009 2:37 pm

Re: Project 1

Postby noobgrammer on Fri Sep 25, 2009 7:14 pm

that's alright was my bad for making it sound like the way you had your if ladder was wrong I was trying to show you a better way but I guess I wrote it out misleading
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Project 1

Postby hoyun on Sat Sep 26, 2009 2:02 am

Well first post in this forum and here's my code. Not fancy stuff like many others and I didn't cover all the pitfalls for user mistakes, but it should work if everything is enter correctly.

Code: Select all
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double num1, num2, answer;
   
    char sign;
   
    cout << "Enter number 1: " << endl;
    cin >> num1;
   
    cout << "Enter sign: " << endl;
    cin >> sign;
       
    cout << "Enter number 2: " << endl;
    cin >> num2;
   
    switch(sign)
    {
          case '+':
               answer = num1 + num2;
               break;
          case '-':
               answer = num1 - num2;
               break;
          case '*':
               answer = num1 * num2;
               break;
          case '/':
               answer = num1 / num2;
               break;
          default:
               cout << "not a sign" << endl;                         
    }
   
    cout << "answer of " << num1 << sign << num2 << " is :" << answer << endl;
   
    system("pause");   
}
hoyun
 
Posts: 1
Joined: Sat Sep 26, 2009 12:39 am

Re: Project 1

Postby JaredC on Wed Oct 07, 2009 9:19 pm

Below is my try at the project.

Would've used switch statments like some people have here but haven't got to that yet, so will leave it out.

However, I wanted to include some user error nets. I have commented one out which I thought in theory would work, but didn't work as intended. (Though no compiler errors).

Thanks,

You're doing a good job antiRTFM and the rest of you guys helping out of course. :)

Jared.

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

int main()
{
    string restart;
    restart = "r";
   
    do {

    cout << "Welcome to the Math Calculation Program!" << endl;
    cout << "Please enter your first number: ";
    float num1;
    cin >> num1;
    cout << "Please select your operation - (+,-,*,/): ";
   
    char choice;
    cin >> choice;
   
    // Why doesn't the commented code below work?
   
   /*  if (choice == '+' || '-' || '*' || '/') {
       cout << "Syntax Error - Please enter a correct operator. \n" << endl << main(); } */
       
       
   
    cout << "Please enter your second number: ";
    float num2;
    cin >> num2;
   
    if (choice == '+')
        cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
        else if (choice == '-')
             cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
             else if (choice == '*')
                  cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
                  else if (choice == '/')
                       cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
                       
                       cout << "Type 'r' to restart. \n";
                       cin >> restart;
                       cout << endl;
                       
                       cin.get();
                       
                       }
                       
    while (restart == "r");
                       
    system("pause");
    return 0;
}
JaredC
 
Posts: 4
Joined: Wed Oct 07, 2009 6:55 pm

Re: Project 1

Postby noobgrammer on Wed Oct 07, 2009 9:28 pm

Your commented out problem is you have to compare choice to each one individually

Code: Select all
if (choice == '+' || choice == '-' || choice == '*' || choice == '/')
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Project 1

Postby JaredC on Thu Oct 08, 2009 5:41 am

noobgrammer wrote:Your commented out problem is you have to compare choice to each one individually

Code: Select all
if (choice == '+' || choice == '-' || choice == '*' || choice == '/')


Okay thanks, but I still seem to be having the same problem as I had before stragely enough. :S

That part of my code is now:

Code: Select all
if (choice != '+' || choice != '-' || choice != '*' || choice != '/') {
       cout << "Syntax Error - Please enter a correct operator. \n" << endl; }


Yet when I want any one of those operators as my 'choice' it still couts that error and continues the program. :?
JaredC
 
Posts: 4
Joined: Wed Oct 07, 2009 6:55 pm

Re: Project 1

Postby noobgrammer on Thu Oct 08, 2009 8:14 am

That part of my code is now:

Code: Select all
    if (choice != '+' || choice != '-' || choice != '*' || choice != '/') {
           cout << "Syntax Error - Please enter a correct operator. \n" << endl; }



Yet when I want any one of those operators as my 'choice' it still couts that error and continues the program. :?


Ok if you look at that and think about it you will see that its true that it is not equal to one of them
A if statement will run its block of code if its condition is true, so it will always be true since it is always not equal to one of them.

Here is a way to do that

Code: Select all
#include <iostream>

int main()
{
   char x;
   
   while(1)//infinite loop
   {
      std::cin>> x;
      
      if (x < '*' || x > '/')
      {
         std::cout<< "error\n";
      }
      else
      {
         if (x == '.' || x == ',')
         {
            std::cout<< "error 2\n";
         }
         else
         {
            break;//passes test so breaks out of loop
         }
      }
   }//end of loop
         std::cout<< "carry on\n";
}



The way I know how to do that is a char is a byte , it can store up to 256 numbers and each number is assigned a character, so these have a unique number assigned to then ('+','-','*','/',) but since they are not grouped completely by them self's there is a (',','.')(comma and a period) mixed in there that had to be checked also before it can pass and move on.

here is a code to run so you can see what I'm talking about

Code: Select all
for (int i = 0; i <= 255; i++)
   {
      std::cout<< i << "\t" << (char)i << "\n";
   }



I think that its a standard and will always be in that order, but I could be wrong.
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Project 1

Postby darmstrong on Sat Oct 10, 2009 11:42 pm

#include "stdafx.h"
#include <iostream>

using namespace std;

int main ()
{
char mathOp;
float numberA;
float numberB;
float answer;

cout << "Welcome to Dan's Simple Calculator! \n";
cout << "(C) Armstrong Dynamics, Inc. (C) 2009-2010 \n";
cout << "(TM) Armstrong Dynamics, Inc." << endl << endl << endl;

cout << "Enter the first number in your equasion... ";

cin >> numberA;

cout << "\nEnter the mathimatical operator... ";

cin >> mathOp;

cout << "\nEnter the second number in your equasion... ";

cin >> numberB;

cout << endl;

if(mathOp == '+')
{
answer = numberA + numberB;
}
else
{
if(mathOp == '-')
{
answer = numberA - numberB;
}
else
{
if(mathOp == '*')
{
answer = numberA * numberB;
}
else
{
if(mathOp == '/')
{
answer = numberA / numberB;
}
}
}
}

cout << numberA << " " << mathOp << " " << numberB << " " << "=" << " " << answer;

int f;
cin >> f;
return 0;
}
darmstrong
 
Posts: 2
Joined: Sat Oct 10, 2009 7:48 pm

Re: Project 1

Postby JaredC on Sun Oct 11, 2009 6:00 pm

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

int main()
{
    string restart;
    restart = "r";
   
    do {
    cout << endl;
    cout << "Welcome to the Math Calculation Program!" << endl;
    cout << "Please enter your first number: ";
    float num1;
    cin >> num1;
    cout << "Please select your operation - (+,-,*,/): ";
   
    char choice;

    cin >> choice;
     
    if (choice < '*' || choice > '/')
    {
       cout << "Syntax Error \n";
       system("pause");
       main();
    }
     else
    {
         if (choice == '.' || choice == ',')
            {
                    cout << "Syntax Error 2 \n";
                    system("pause");
                    main();
            }
    else
        {
            break;
        }
    }
   
   
    cout << "Please enter your second number: ";
    float num2;
    cin >> num2;
   
    if (choice == '+')
        cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
        else if (choice == '-')
             cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
             else
                  if (choice == '*')
                     cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
                  else
                       if (choice == '/')
                          cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
                       
                       cout << "Type 'r' to restart. \n";
                       cin >> restart;
                       cout << endl;
                       
                       cin.get();
                       
                       }
                       
    while (restart == "r");
                       
    system("pause");
    return 0;
}


Well,

Learnt something new, thanks. :) Didn't know operators could be used like that. Helped me solve the issue. Thinking of doing the same thing with if the user enters something invalid for the numbers also..But not sure if I'm venturing further than I actually should be. :P

Again, left out the switch statments. Will get round to them eventually!

Jared.
JaredC
 
Posts: 4
Joined: Wed Oct 07, 2009 6:55 pm

Re: Project 1

Postby noobgrammer on Mon Oct 12, 2009 11:06 am

A good rule is to test your project to see if it works

You almost have it right you forgot to add a while loop so if you fail it will loop back and try again

The problem you are haveing is that break means it will break out of a loop and it breaks out of your do...while loop instead so you never get to enter the second number

here's how to fix that and get rid of main recursion also

Code: Select all
while(1) //I added a while here
   {
      cout << "Please select your operation - (+,-,*,/): ";
        cin >> choice;
         
        if (choice < '*' || choice > '/')
        {
           cout << "Syntax Error \n";
           //system("pause"); //these are not necessary
           //main();  //these are not necessary
        }
         else
        {
             if (choice == '.' || choice == ',')
                {
                        cout << "Syntax Error 2 \n";
                        //system("pause"); //these are not necessary
                        //main(); //these are not necessary
                }
        else
            {
                break;// break will break you out of the loop
            }
        }
   }//end of while loop
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Project 1

Postby JaredC on Mon Oct 12, 2009 11:57 am

*facepalm*

Yep. I understand what you are saying and have applied it to my code now. I have no idea why I went the route I took before. I swear I actually did what you've just posted now, but I had problems so I went with that route instead..

But nevermind, I've studied it and understand what's happening. Sorry for the recursive noobishness, I'm appreciative of your patience. :)

Thanks,

Jared.
JaredC
 
Posts: 4
Joined: Wed Oct 07, 2009 6:55 pm

Re: Project 1

Postby jwright on Wed Oct 21, 2009 11:57 am

Hey guys, cool forum =)
Well since loads of people are posting their codes for this one I thought hey what the heck:



Code: Select all
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
   int firstNumber;
   int secondNumber;
   char operation;

   cout << "Please enter a number:";
   cin >> firstNumber;
   cout << "\nPlease choose one of the following operations: + - * /:";
   cin >> operation;
   cout << "\nPlease choose a second number:";
   cin >> secondNumber;

   cout << firstNumber << operation << secondNumber << '=';

   if (operation == '+')
      cout << firstNumber+secondNumber;
   else
      if (operation == '-')
         cout << firstNumber-secondNumber;
      else
         if (operation == '*')
            cout << firstNumber*secondNumber;
         else
            if (operation == '/')
               cout << firstNumber/secondNumber << "remainder" << firstNumber%secondNumber;
            else
               cout << "\nYou entered incorrect value type/s. Please re-run the program and follow the instructions";

   cout << "\nPlease enter some data and press enter to exit:";


   char f;
   cin >> f;
    return 0;
}
jwright
 
Posts: 7
Joined: Wed Oct 21, 2009 11:47 am
Location: Essex, England

Re: Project 1

Postby thanhquanky on Sun Nov 08, 2009 2:32 pm

Code: Select all
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    float so1, so2, kq;
    char dau;
   
    cout << "My Calculator v1" << endl;
    cout << "Please enter the first number: ";
    cin >> so1;
    cout << "Please enter the second number: ";
    cin >> so2;
    cout << "Please enter one of these operators (+,-,*,/) : ";
    cin >> dau;
    cout << "Your expression is :" << endl << so1 << " " << dau << " " << so2 << endl;
    cout << "Result is: ";
    switch (dau) {
           case '+':
                kq = so1 + so2;
                cout << kq;
                break;
           case '-':
                kq = so1 - so2;
                cout << kq;
                break;
           case '*':
                kq = so1 * so2;
                cout << kq;
                break;
           case '/':
                if (so2!=0) {
                   kq = so1 / so2;
                   cout << kq;
                }
                else
                    cout << "Division by 0";                                                                         
    }
    cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
thanhquanky
 
Posts: 1
Joined: Sun Nov 08, 2009 2:23 pm

Re: Project 1

Postby Lemony Lime on Fri Nov 13, 2009 4:47 am

I've been having trouble with loops, especially if it is looping more than one line. I might not be to that point yet in the videos, but I have no clue how to do it atm. To fix that, I just called back to main() from within itself. Is that ever going to be a problem?

I also tried to add a feature that told you that what you typed in was invalid when it was something that wasn't a number between the possible values for a long int, or if you typed in an operation that wasn't a char that's used in the if statements. It works for characters, but not for numbers. I was hoping that it would realize that pizza (or whatever it is that you decide to type) isn't between the max and min values, and just tell you to start over, but it glitches it. Finally, I'm using "\n\n" to skip a line. Is there something made specifically for that, or am I fine to keep doing it like I have been?

Oh yeah... there was another thing that I wanted to try, but didn't even know where to start... How would I make the answer that is written up previously into a usable option. Like, type:
3+6
3 + 6 = 9
answer*3
answer * 3 = 27

This would be especially useful If I were to generate a random number (which I cheated and looked up how to do ahead of time) and wanted that random number to be either 1 or 2. If it's 1, the first option is selected, and if it's 2, the second is selected.

Code: Select all
#include "stdafx.h"
#include <iostream>

using namespace std;



int main()
{
   char op;
   double num1;
   double num2;



   cout << "Enter 1st number.\n\n";
   cin >> num1;
   cout << "\nSelect operation.\n\n";
   cin >> op;
   cout << "\nEnter 2nd number.\n\n";
   cin >> num2;
   

   if ( op == '+' && num1 <= 4294967295 && num1 >= -4294967295 && num2 <= 4294967295 && num2 >= -4294967295 )
      cout << "\n" << num1 << " + " << num2 << " = " << num1 + num2 << "\n\n";
   else
      if ( op == '-' && num1 <= 4294967295 && num1 >= -4294967295 && num2 <= 4294967295 && num2 >= -4294967295 )
         cout << "\n" << num1 << " - " << num2 << " = " << num1 - num2 << "\n\n";
      else
         if ( op == '*' && num1 <= 4294967295 && num1 >= -4294967295 && num2 <= 4294967295 && num2 >= -4294967295 )
            cout << "\n" << num1 << " * " << num2 << " = " << num1 * num2 << "\n\n";
         else
            if ( op == '/' && num1 <= 4294967295 && num1 >= -4294967295 && num2 <= 4294967295 && num2 >= -4294967295 )
               cout << "\n" << num1 << " / " << num2 << " = " << num1 / num2 << "\n\n";
            else
            {
               cout << "Invalid Number or Operation.\n\n";
               main();
            }

   main();
}
Lemony Lime
 
Posts: 1
Joined: Fri Nov 13, 2009 4:17 am

PreviousNext

Return to Projects

Who is online

Users browsing this forum: No registered users and 0 guests