4 Replies - 679 Views - Last Post: 30 July 2010 - 08:26 PM Rate Topic: -----

#1 galoochi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-July 10

Making a calculator

Posted 30 July 2010 - 06:37 PM

I'm writing a calculator and having issues. Everytime I run it it says my variables aren't being initialized. Can anyone see what's wrong?

#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;

void scan_data(char, double);
void do_next_op(double, char, double);
 
int main()
{
    double acc;
    double num;
	char op;

	acc=0.00;
	
	do
	{
		scan_data(op, num);

		do_next_op(acc, op, num);
	}
	while (op != 'q');

	system ("pause");

}

void scan_data(char op, double num)
{
	cout << "Enter an operand (+ - * / ^) and a number, or q to quit: " << endl;
	cin >> op >> num;
}

void do_next_op(double acc, char op, double num)
{
	switch(op)
	{
	case '+':
		acc = acc + num;
		break;

	case '-':
		acc = acc - num;
		break;

	case '*':
		acc = acc * num;
		break;

	case '/':
		acc = acc / num;
		break;

	case '^':
		acc = pow(acc, num);
		break;
	}
	cout << "result so far is " << acc << endl;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Making a calculator

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Making a calculator

Posted 30 July 2010 - 06:41 PM

Well at a quick glance I see 2 variables that arent initialized before they're used: num and op. YOu have these 2 lines

double acc;
char op;



But nowhere in your code do I see them given a value before you try to use them. Declaring a variables isnt the same as initializing it, to initialize it you need to give it an initial value before you can use it
Was This Post Helpful? 0
  • +
  • -

#3 galoochi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-July 10

Re: Making a calculator

Posted 30 July 2010 - 06:46 PM

Never mind, I got it!

So just to make sure I understand WHY it's working now...here's what I changed to make it work
void scan_data(char&, double&);
void do_next_op(double&, char&, double&);

So does that basically make it so that the variables are initialized by the inputs in the functions?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 galoochi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-July 10

Re: Making a calculator

Posted 30 July 2010 - 07:02 PM

Ugh, feeling stupid but one more question. The program is supposed to output "final result is:" after the user enters 'q' to quit. I tried putting it in like this
if (op == 'q')
	{
		cout << "final result is " << acc << endl;
	}

but nothing is outputting after q is entered.
Was This Post Helpful? 0
  • +
  • -

#5 DJPlayer  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 73
  • Joined: 19-September 07

Re: Making a calculator

Posted 30 July 2010 - 08:26 PM

I don't see why you need to pass by reference for your functions..

also where are you trying to output your acc variable? Your function is of type void.. so you're not going to be able cout the proper number from main w/o returning it in the function.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1