Help - Search - Members - Calendar
Full Version: C++
Sal's RuneScape Forum > Everything... Not RuneScape > Tech Talk > Programming & Web Development
Jr Josh
I've started an online class with my school, which is C++. This is one more class than normal so it seems like a big load to me.

Anyway, I've started out on C++ and I had some questions.

The first assignment I had to do was to make a program using if statements:

CODE
#include <iostream>    

using namespace std;
        
int main()                                                  // Most important part of the program!
{
  int points;                                               // Need a variable...
  
  cout<<"Please input your number of points: ";             // Asks for number of points
  cin>> points;                                             // The input is put in points
  cin.ignore();                                             // Throw away enter
  if ( points < 25 ) {                                      // If the points is less than 25
     cout<<"Sorry you have too few points to qualify!\n";    // Just to show you it works...
  }
  else if ( points == 25 ) {                                // I use else just to show an example
     cout<<"Good job, you met qualification!\n";             // Just to show you it works...
  }
  else {
    cout<<"Wow, you made it with ease!\n";                      // Executed if no other statement is
  }
  cin.get();
}


The next one was doing math with already set integers:

CODE
#include <iostream>
using namespace std;
int main()

{
    int x = 10;
    int y = 2;
    int sum, difference, product, quotient;
    sum = x + y;
    difference = x - y;
    product = x * y;
    quotient = x / y;
    cout<< "The sum of" << x << "&" << y << "is" << sum << "." << endl;
    cout<< "The difference of" << x << "&" << y << "is" << difference << "." << endl;
    cout<< "The product of" << x << "&" << y << "is" << product << "." << endl;
    cout<< "The quotient of" << x << "&" << y << "is" << quotient << "." << endl;
    system ("pause");
}


I got both of those. The next assignment is to make a calculator from just those two assignments. I'm trying to get a feel for it by googling C++ calculators, but they are all advanced and have functions that I haven't even learned yet.

Any help on how I would start it out and I should be able to manage the rest?
Roy
I only had a brief intro to c++ (started learning it, then switched to c#) so don't expect much from me... For a calculator in console (I assume it isn't with forms etc) you will need to ask the user what he wants to do (+, -, *, / ?), then ask for two values to work with, do the math and output it.

Like I said I only got a brief intro of c++ but I tried something:
CODE
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()

{
    int x;
    int y;
    int choice;

    cout<< "1) +\n2) -\n3) *\n4) /\n? ";
    cin>> choice;
    cout<< "Give 2 numbers to work with: ";
    cin>> x;
    cin>> y;

    if  (choice == 1)
    {
        cout<< x << " + " << y << " = " << x + y << "." << endl;
    }
    else if (choice == 2)
    {
        cout<< x << " - " << y << " = " << x - y << "." << endl;
    }
    else if (choice == 3)
    {
        cout<< x << " * " << y << " = " << x * y << "." << endl;
    }
    else
    {
        cout<< x << " / " << y << " = " << x / y << "." << endl;
    }

    system ("pause");
}


Haven't tested this so it might give errors and of course to learn how to program in c++ you shouldn't by just copy-pasting this (try reading it instead)...
Jr Josh
That worked for me.

I figured I would just post in here again since it hasn't been seven days and my teacher hasn't emailed me back about the issue.

CODE
// Program to determine the least number of coins in making change
#include <iostream>
using namespace std;

// the following four lines of code establish our intergers and values
int HalfDollar = 50;
int Quarter = 25;
int Dime = 10;
int Nickel = 5;

int main()
{

// Establishes change as a field we will calculate
int change;

// Note do not enter edited values like $5.83 ENTER 583
cout<<"Enter change in cents: (whole number with no editing) ";
cin>>change;
cout<<

// Note line 23 calculate the number of Half Dollars
cout<<"The number of Half Dollars is "<<
// Note line 25 places the remainder into the field named change
change = change % HalfDollar;

cout<<"The number of Quarters is "<<
change = change % Quarter;

cout<<"The number of Dimes is "<<
change = change % Dime;

cout<<"The number of Nickels "<<
change = change % Nickel;

cout<<"The number of Pennys is "<<
system("Pause");
}


Just starting to learn this, so if someone could, I would like to know what these errors mean and how to fix this.

Thanks.
cjgone
What exactly..is wrong other then you aren't printing out the number of each coin.
Jr Josh
Did you even try compiling and running it?
cjgone
No, but the program looks fine to me

heres the quick optimalish way.
CODE
int amount;
cin >> amount;
int values[] = { 50, 25, 10, 5, 1 };
string coins = { "fiftycents", "quarters", "dimes", "nickels", "pennies" };

for ( int count = 0; count < 5; count ++)
{
cout << coins[count] << endl;
cout << amount / values[count] << endl;
amount = amount % values[count];
}
Jr Josh
None of them work. I just want to get the one the instructor gave me to work.
cjgone
get an input and copy this into your code:

CODE
//int amount = input value
cout << amount / 50 << endl;
amount = amount % 50;
cout << amount / 25 << endl;
amount = amount %25;
cout << amount / 10 << endl;
amount = amount % 10;
cout << amount / 5 << endl;
amount = amount % 5;
cout << amount / 1 << endl;


Your code obviously doesn't work. Half of the code lines HAVE been commented out and deleted and replaced with a comment.

Well, lets look at your code:
CODE
cout<<

// Note line 23 calculate the number of Half Dollars
cout<<"The number of Half Dollars is "<<
// Note line 25 places the remainder into the field named change


Hmm.. Well, there's a broken cout statement at the top that probably gives a compiler error, there's no semicolon end at the end of the next cout statement, wonder why that'd be. Now you read the comment on the line and it tells you that it's calculating how many half-dollars.... but it doesn't, so you can come to the conclusion that the line wasn't finished, but was meant for YOU to finish. So the statement should be:

CODE
cout<<"The number of Half Dollars is "<< change / 50;


You have to do this with every other cout statement too.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.