what am I doing wrong? please help

Hi, I am new to programming and I cant figure out what I am doing wrong.



I sure bought the wrong book to self teach myself. haha

This is the problem i need to solve.
i. Prompt the user for a numerator value.

ii. Store the user-entered value in a variable numerator.

iii. Prompt the user for a denominator value.

iv. Store the user-entered value in a variable denominator.

v. Call the fractionToDecimal() function, passing numerator and denominator as arguments.

vi. Display the value returned by the fractionToDecimal() function along with a message like "The fraction 3/4 is equal to the the decimal value 0.75".





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "stdafx.h"
#include <iostream>
#include <cmath>



using namespace std;


int main() {

	
	double  numerator; 
	double  denominator;
	char choice;
	bool quit = false;
	
	
	do { 
		
		cout<<("A. Add two fractions \nB. Convert a fraction to decimal \nC. Multiply two fractions \nQ. Quit");
		cin>>choice;
		
		switch(choice){
		
		case 'A':
			cout<<("adding two fractions")<<endl;
			break;

		case 'B': 
			cout<<("Please enter a numerator")<<endl;
			cin>> numerator;
			
			cout<<("Please enter a denominator")<<endl;
			cin>> denominator;

			double q2 = fractionToDecimal(numerator,denominator);
			cout<<q2;

			break;

		case 'C': 
			cout<<("Multiply two fractions")<<endl;
			break;

		case 'Q': 
			cout<<("Quit")<<endl;
			quit = true;
			break;
		}
	
	}while (quit != false);


	system("pause");

	return 0;
	
	
}

double fractionToDecimal(int numerator, int denominator){

	double quotient;
	if (denominator == 0){
	
		cout<<("This is invalid");
	    quotient = 0;
	}
	else {
		
		quotient = numerator / denominator;
	}
	return quotient;

}




error messages i'm getting:

fractionToDecimal': identifier not found
(51)initialization of 'q2' is skipped by 'case' label
(42) : see declaration of 'q2'
The compiler reads your code top to bottom, so when it gets to line 37, it doesn't know yet what fractionToDecimal is. If you keep the function definition after main, you need to put a function prototype or declaration before the main function (and after the preprocessor directives). This tells the compiler basic information about the function.

double fractionToDecimal(int, int);


I might consider validating the entry of the denominator before sending it to the function, to allow the user to re-enter a valid number.

edit - other good points in posts below
Last edited on
Line 8: You need a forward declaration of fractionToDecimal.

Line 62: You have a type mismatch between the call to fractionToDecimal and the actual function. In one place your arguments are doubles, while in the function they are ints.

Line 37: Break this into two statements.
1
2
  double q2;
  q2 = fractionToDecimal(numerator,denominator);



You need to declare the function prototype at the top (before the main() like:

double fractionToDecimal(int, int );

on line 37 dont declare a variable inside a case, declare it at the top of your function.

Hmmm, I cant seem to get it working. Could someone show me the answer? I would GREATLY appreciate it :)

Thank you everyone for trying to help me out here.
closed account (j3Rz8vqX)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// declaring functions prototypes
#include <iostream>
using namespace std;

void odd (int x);//<-----------------------Function prototype!
void even (int x);//<----------------------Function prototype!

int main()
{
  int i;
  do {
    cout << "Please, enter number (0 to exit): ";
    cin >> i;
    odd (i);//<----------------------------------------Function call
  } while (i!=0);
  return 0;
}

void odd (int x)//<---------------------------------Function definition!
{
  if ((x%2)!=0) cout << "It is odd.\n";
  else even (x);
}

void even (int x)//<-------------------------------Function definition!
{
  if ((x%2)==0) cout << "It is even.\n";
  else odd (x);
}

Example from the tutorials of this site; with minor commenting.
http://www.cplusplus.com/doc/tutorial/functions/

Go to the "Declaring functions" section.

Use your browsers search function to search for "Declaring functions".

Another source:http://www.tutorialspoint.com/cplusplus/cpp_functions.htm
Topic archived. No new replies allowed.