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 liquid on Thu Sep 03, 2009 2:11 am

This is my attempt. I still haven't figured out how to fix the crash/infinite loop that happens when I type a character instead of a number but other than that it should be working properly.
So how do I tell the compiler what to do if the user inputs a character instead of a number?

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

int main()
{
begin:
   double number1 = 0;
   int choice;
   while ((cout << "\nPlease input a number: ") && !(cin >> number1))
{
   cout << "This is not a number!";
   cin.clear();
   cin.ignore(numeric_limits<streamsize>::max(), '\n');
}   
   
   cout << "Please choose an operation you'd like to perform:\n";
   cout << "1. Addition\n";
   cout << "2. Subtraction\n";
   cout << "3. Multiplication\n";
   cout << "4. Division\n";
     
again:
   while ((cout << "Choose operation: ") && !(cin >> choice) || (choice >=5) || (choice <0))
{
      cout << "This is not a valid choice, try again!\n";
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
     goto again;
}   
 
   double number2 = 0;
 
   while ((cout << "\nNow input a second number: ") && !(cin >> number2))
{
     cout << "This is not a valid choice!";
     cin.clear();
     cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

   if (choice == 1)
      cout << number1 << " + " << number2 << " = "<< number1 + number2 << endl;
   if (choice == 2)
      cout << number1 << " - " << number2 << " = "<< number1 - number2 << endl;
   if (choice == 3)
      cout << number1 << " * " << number2 << " = "<< number1 * number2 << endl;
   if (choice == 4)
{
      if (number2 == 0)
         cout << "Cannot divide by zero.\n";
      else
         cout << number1 << " / " << number2 << " = "<< number1 / number2 << endl;
   }   
     
   char response;
   cout << "Would you like to start again? (y/n): ";
   cin >> response;
   if (response == 'y')
   {
      system("cls");
   goto begin;
   }
   else
      cout << "\nHave a nice day!" << endl;

   system("pause");
      return 0;
}
Last edited by liquid on Fri Sep 04, 2009 1:40 am, edited 5 times in total.
liquid
 
Posts: 3
Joined: Wed Sep 02, 2009 7:36 am

Re: Project 1

Postby C++ on Thu Sep 03, 2009 6:31 am

When you request a type from the input stream that is not the requested type, or it can't be implicitly converted, then the input stream will go into a fail state.

Read at least paragraphs 15.1 through 15.7 here for important information about in- and output.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: Project 1

Postby liquid on Fri Sep 04, 2009 1:32 am

Thanks, that was useful. I updated the code but I still don't quite get why
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
works, what does
!(cin >> choice)
mean and why it doesn't work without the ! operator(probably something to do with some boolean stuff). Actually, I understand what it means but I don't get it. How does ! change (cin >> choice)?
I wouldn't be able to call the cin.ignore... stuff on my own so I had to copy-paste and adjust to my needs. I wouldn't be able to even do a proper search to find the things I need to #include.
Also, I can still make the program go into something similar to a fail state when I type in "2a" for instance. The calculation is done but it still goes directly through the code to system("pause"); without asking me anything.
I do hope, though, that this is something completely normal for someone who has watched 20 vids in two days and hopefully I will catch up in the coming days/weeks/months.
Oh, and btw - I love when things are going well even if it's slow. It's addictive. Unlike when I couldn't get anything other than "Hello World!" and become frustrated and throw stuff at the wall. antiRTFM - you are my hero! :)
liquid
 
Posts: 3
Joined: Wed Sep 02, 2009 7:36 am

Re: Project 1

Postby C++ on Fri Sep 04, 2009 6:48 am

To find which headers to use, search through the documentation for your compiler. For Visual Studio C++ you can look through the MSDN.
You can find information on numeric_limits here

Code: Select all
!(cin >> choice)


I'll explain in steps how it gets evaluated.
1)
Code: Select all
(cin >> choice)


gets a value from the input stream and returns a reference to itself.
It returns a reference to itself because you can do this

Code: Select all
!(cin >> choice >> choice2)


2) Now we have

Code: Select all
!cin


The compiler has no idea how to implicitly convert a non built in object to a boolean or other built in type that can be implicitly converted to boolean.
That's why cin uses operator ! which it inherits from ios.
It then calls operator !
That operator returns the state of the input stream.
operator !

If the user enters a valid number but invalid characters after the number, for example '99adflkhsfkshdfjshdf'
The input stream will extract the two 9 digits, but will leave the other non numerical characters in the input stream. Important to know is that the input stream will not go into a fail state. To fix this you should write the following code

Code: Select all
while (cout << "Please enter a number: " && !(cin >> input))
{
   cout << "That's not a valid number!\n";
   cin.clear();
   cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // This will clear the input stream of any trailing non numerical characters.


liquid wrote:I do hope, though, that this is something completely normal for someone who has watched 20 vids in two days and hopefully I will catch up in the coming days/weeks/months.


You're ahead of the average person because you actually stop and think about what your program actually does. You found that just asking for input does not work very well if the user provides bad input.
I have demonstrated how to get valid user input a lot of times, and still there are plenty of people here who just don't get it, or refuse to learn and implement it. So don't worry, you're doing just fine!
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: Project 1

Postby oasis on Sat Sep 05, 2009 3:02 am

here is my code....it's a bit messy but it works :-)
Code: Select all
#include<iostream>
using namespace std;
void error()
{
     cout << "there has been an error, please try again";
     }

int main()
{
    float answer1 = 0;
    float answer2 = 0;
    float result = 0;
    char op;
    char a = '+';
    char b = '-';
    char c = '*';
    char d = '/';
   
    cout << "\n\nkey:\n\n";
    cout << "+ = addition\n";
    cout << "- - subtraction\n";
    cout << "/ = devide\n";
    cout << "* = multipy\n\n";
   
    cout << "Enter a number: ";
    cin >> answer1;
   
    cout << "\nEnter the operater you would like to use: ";
    cin >> op;
   
    cout << "\nEnter your second number: ";
    cin >> answer2;
   
    if (op == a)
    {
          result = answer1 + answer2;
          cout << answer1; cout << " + "; cout << answer2; cout << " = "; cout << result;
          main();
           }
    else if (op == b)
    {
         result = answer1 - answer2;
         cout << answer1; cout << " - "; cout << answer2; cout << " = "; cout << result;
         main();
         }
    else if (op == c)
    {
         result = answer1 * answer2;
         cout << answer1; cout << " x "; cout << answer2; cout << " = "; cout << result;
         main();
         }
    else if (op == d)
    {
    result = answer1 / answer2;
    cout << answer1; cout << " / "; cout << answer2; cout << " = "; cout << result;
    main();
}
else
{
   error();
}
           char f;
           cin >> f;
           return 0;
           }
           
oasis
 
Posts: 1
Joined: Mon Aug 31, 2009 11:29 am

Re: Project 1

Postby noobgrammer on Sat Sep 05, 2009 7:51 am

your stuck in a loop unless you type something wrong
I don't believe calling main is proper coding in that you could use a while or do while loop
check liquids post today he has a good error checker also

other than that it works fine
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 Thu Sep 17, 2009 2:44 pm

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




int main()
{
   float usersNumber1;
   float usersNumber2;
   char desiredFunction;
   
   
   
   
   cout << "Simple calculator!\n";
   cout << "Please enter your first number: \n";
   cin >> usersNumber1;
   cout << "Please enter your desired function. (*,/,+,-)\n";
   cin >> desiredFunction;
   cout << "Please enter your second number: \n";
   cin >> usersNumber2;
   cout << endl;
   cout << "Thankyou.\n Your answer is: ";

   if ( desiredFunction == '*' )
      cout << usersNumber1 * usersNumber2;
      
   if ( desiredFunction == '/' )
      cout << usersNumber1 / usersNumber2;
         
   if ( desiredFunction == '+' )
      cout << usersNumber1 + usersNumber2;

   if ( desiredFunction == '-' )
      cout << usersNumber1 - usersNumber2;

   cout << "\n\n\n\nType any letter followed by enter to exit.";

   char f;
   cin >> f;

   return 0;
}


It works al right I guess...
I'm only on tutorial 25 so idk many ways to make it any better... lol
cawwum
 
Posts: 5
Joined: Thu Sep 17, 2009 2:37 pm

Re: Project 1

Postby Cono on Fri Sep 18, 2009 1:43 am

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

using namespace std;

int main()
{
   char response ;
   float firstnumber ;
   float secondnumber ;
   string cont;
   
   cont = "yes";

   
   
   cout << "Enter the first number \n";
   cin >> firstnumber ;
   cout << "Enter which operation you want to do \n";
   cin >> response ;
   cout << "Enter the second number \n";
   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 << "Do you want to continue? \n";
   cin >> cont;
   if ( cont == "yes" )
   {
      main();
   }
   else

   return 0;
}
Cono
 
Posts: 1
Joined: Fri Sep 18, 2009 1:01 am

Re: Project 1

Postby masterkey on Tue Sep 22, 2009 6:07 pm

#include <iostream>


using namespace std;

int main()

{
float x;

float y;

char z;

cout <<"calculator \n\n";

cout<<"plaese enter your first number\n";
cin>> x;


cout <<"type in one of the following mathmatical operaters\n";
cout<<"+, -, /, *\n";
cin>> z;

cout<<"please enter a second number\n";
cin>> y;
switch(z)
{
case '+':


cout<<x << "+"<< y << "=" <<x + y<<"\n";
break;
case '-':


cout<<x <<"-"<< y << "=" <<x - y<<"\n";
break;
case '/':



cout<<x <<"/"<<y<< "=" <<x / y<<"\n";

break;
case '*':


cout<<x <<"*"<<y<< "=" <<x * y<<"\n";
break;
}
system("pause");

return 0;

}
masterkey
 
Posts: 19
Joined: Tue Sep 22, 2009 5:31 pm

Re: Project 1

Postby masterkey on Tue Sep 22, 2009 6:09 pm

this seems to work for me
masterkey
 
Posts: 19
Joined: Tue Sep 22, 2009 5:31 pm

Re: Project 1

Postby cawwum on Wed Sep 23, 2009 1:55 pm

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


void exit()
{
   return;
}
void Calculator1()
{
   float firstNumber;
   float secondNumber;
   char function;
   char Exit;
   
   cout << "Enter your first number: ";
   cin >> firstNumber;
   cout << "Enter your function (+,-,/,*): ";
   cin >> function;
   cout << "Enter your second number: ";
   cin >> secondNumber;
   cout << "The answer is: ";

   if (function == '+')
      cout << firstNumber + secondNumber;
   if (function == '/')
      cout << firstNumber / secondNumber;
   if (function == '-')
      cout << firstNumber - secondNumber;
   if (function == '*')
      cout << firstNumber * secondNumber;

   cout << "\n\n\n Exit? (Y/N)";
   cin >> Exit;

   if (Exit == 'y')
      exit();
   else
      Calculator1();

}

void Calculator2()
{   
   float firstNumber;
   float secondNumber;
   float thirdNumber;
   char function;
   char secondFunction;
   char Exit;


   cout << "Enter your first number: ";
   cin >> firstNumber;
   cout << "Enter your function (+,-,/,*): ";
   cin >> function;
   cout << "Enter your second number: ";
   cin >> secondNumber;
   cout << "Enter your second function (+,-,/,*): ";
   cin >> secondFunction;
   cout << "Enter your third number: ";
   cin >> thirdNumber;
   cout << "The answer is: ";

   if (function == '+', secondFunction == '+')
      cout << firstNumber + secondNumber + thirdNumber;
   if (function == '+', secondFunction == '-')
      cout << firstNumber + secondNumber - thirdNumber;
   if (function == '+', secondFunction == '/')
      cout << firstNumber + secondNumber / thirdNumber;
   if (function == '+', secondFunction == '*')
      cout << firstNumber + secondNumber * thirdNumber;
   
   if (function == '-', secondFunction == '+')
      cout << firstNumber - secondNumber + thirdNumber;
   if (function == '-', secondFunction == '-')
      cout << firstNumber - secondNumber - thirdNumber;
   if (function == '-', secondFunction == '/')
      cout << firstNumber - secondNumber / thirdNumber;
   if (function == '-', secondFunction == '*')
      cout << firstNumber - secondNumber * thirdNumber;

   if (function == '/', secondFunction == '+')
      cout << firstNumber / secondNumber + thirdNumber;
   if (function == '/', secondFunction == '-')
      cout << firstNumber / secondNumber - thirdNumber;
   if (function == '/', secondFunction == '/')
      cout << firstNumber / secondNumber / thirdNumber;
   if (function == '/', secondFunction == '*')
      cout << firstNumber / secondNumber * thirdNumber;
   
   if (function == '*', secondFunction == '+')
      cout << firstNumber * secondNumber + thirdNumber;
   if (function == '*', secondFunction == '-')
      cout << firstNumber * secondNumber - thirdNumber;
   if (function == '*', secondFunction == '/')
      cout << firstNumber * secondNumber / thirdNumber;
   if (function == '*', secondFunction == '*')
      cout << firstNumber * secondNumber * thirdNumber;

   cout << "\n\n\n Exit? (Y/N)";
   cin >> Exit;

   if (Exit == 'y')
      exit();
   else
      Calculator2();



}

void CalculatorPrep()
{
   short int amountOfNumbers;



   cout << "How many numbers will your sum include? (2-3)";
   cin >> amountOfNumbers;

   if (amountOfNumbers <= 2)
      Calculator1();
   if (amountOfNumbers >= 3)
      Calculator2();
   char f;
   cin >> f;
   return;

}

void RangeFinder()
{
   float firstNumber;
   float secondNumber;
   char Exit;
   
   
   cout << "Type your first number: ";
   cin >> firstNumber;
   cout << "Type your second number: ";
   cin >> secondNumber;
   cout << "\n\nThe answer is: ";

   if (firstNumber > secondNumber)
      cout << firstNumber - secondNumber;
   else
      cout << secondNumber - firstNumber;

   cout << "\n\n\n Exit? (Y/N)";
   cin >> Exit;

   if (Exit == 'y')
      exit();
   else
      RangeFinder();

}

int main()
{
   int Decision;


   cout << "1. Calculator\n2. Range Finder";
   cin >> Decision;

   if (Decision == 1)
      CalculatorPrep();
   else
      RangeFinder();

   return 0;
}


uhh, Calculator w/range finder.. It's meant to have a 3-number calculator as well, but when I try to use it, it comes up with the wrong answer... so w/e, I will fix it eventually <:
cawwum
 
Posts: 5
Joined: Thu Sep 17, 2009 2:37 pm

Re: Project 1

Postby noobgrammer on Wed Sep 23, 2009 6:19 pm

Not a bad idea on useing more than two numbers just remember to code for order of operations [edit]my bad compiler already allows for the order[/edit]

the problem with your code is your if statements

heres a couple lines of yours

Code: Select all
if (function == '+', secondFunction == '+')
      cout << firstNumber + secondNumber + thirdNumber;
   if (function == '+', secondFunction == '-')
      cout << firstNumber + secondNumber - thirdNumber;
   if (function == '+', secondFunction == '/')
      cout << firstNumber + secondNumber / thirdNumber;
   if (function == '+', secondFunction == '*')
      cout << firstNumber + secondNumber * thirdNumber;


heres how it should have been done

Code: Select all
if (function == '+' && secondFunction == '+')
      cout << firstNumber + secondNumber + thirdNumber;
   else if (function == '+' && secondFunction == '-')
      cout << firstNumber + secondNumber - thirdNumber;
   else if (function == '+' && secondFunction == '/')
      cout << firstNumber + secondNumber / thirdNumber;
   else if (function == '+' && secondFunction == '*')
      cout << firstNumber + secondNumber * thirdNumber;


you where using commas (,) instead of ands (&&) and not using the else if ladder it was just reading down each one that has a matching character
other than that it looks alright
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 Thu Sep 24, 2009 1:45 pm

Ahh thanks..
I couldn't remember the operator to make it check both... (and with my internet being terrible atm) I guessed it to be ',' when it was really "&&".

I thought that if it didn't find the first if statement it would just ignore it and go onto the next one... so there was no real need to put else.. well it worked on the 2 number calculator..
oh well thanks for the help :)
cawwum
 
Posts: 5
Joined: Thu Sep 17, 2009 2:37 pm

Re: Project 1

Postby noobgrammer on Thu Sep 24, 2009 2:26 pm

with a if else if ladder what happens is

Code: Select all
if (condition)  if this condition is true
do something
if (condition) it will still look at this condition
do something
if (condition)  this one
do something
if (condition)  and this one
do something



where as when you use else ifs

Code: Select all
if (condition) if this one is say false
do something
else if (condition) we move to this and this one is true
do something
else if (condition) it will skip this
do something
else if (condition) this
do something
else if (condition) and this one
do something
else
do this        in a else if ladder if none pass then it would call the else statement


so you see the else if is faster and can help prevent double readings
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 12:33 pm

are you sure its not like

if (whatever)
do something if its true, if not move on and do nothing with this line;

Well anyway... The calculator works perfectly with just

if (function == '+' && secondFunction == '+')
cout << firstNumber + secondNumber + thirdNumber;
if (function == '+' && secondFunction == '-')
cout << firstNumber + secondNumber - thirdNumber;
if (function == '+' && secondFunction == '/')
cout << firstNumber + secondNumber / thirdNumber;
if (function == '+' && secondFunction == '*')
cout << firstNumber + secondNumber * thirdNumber;

etc. etc. etc...
In this case im pretty sure the else makes no difference, it can be there or it can not be..
cawwum
 
Posts: 5
Joined: Thu Sep 17, 2009 2:37 pm

PreviousNext

Return to Projects

Who is online

Users browsing this forum: No registered users and 0 guests