Changing my program to use functions.

I am very new to programming and I am not sure if this is right. Can anyone help me out and tell me what I am doing wrong?

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This program prints addition and muiltiplication tables for
// the range of numbers entered. The numbers must be positive 
// and the upper value of the range must be greater then the lower 
// value of the range.

#include <iostream>
#include <iomanip>

using namespace std;
void enterRange();
void printAdditionTable();
void printMultipicationTable();
void quit();

int main()
{
  char choice;
  int i, j, low = 0, high = 0;
  cout  << setprecision(0);
  do {
    cout << "\n\t\tArithmetic Table Printing\n"   // Print menu.
         << "\tR = Enter Range\n"
         << "\tA = Print Addition Table\n"
         << "\tM = Print Multiplication Table\n"
         << "\tQ = Quit\n\n"
         << "Enter your choice: ";
    cin >> choice;
    cout << "\n\n";
    }
    
    enterRange()
    {
    
      case('R') :
      case('r') : cout << "Enter low number: ";   // Get high and low numbers.
                  cin  >> low;
                  cout << "Enter high number: ";  
                  cin  >> high;
                  if ((low >= 1) && (high > low)   // If range is good, proceed.
                       && (high < 32))  
                    break;                         // Otherwise, process the error.
                  else if (high <= low) {
                    cout << "Error! High number must be greater than Low number.\n\n";
                  } else if (high <= 1) {
                    cout << "Error! High number must be greater than 1.\n\n";
                   }
                   else if (low <= 0) {
                   cout << "Error! Low number must be greater than 0.\n\n";
                  } else if (high > 31) {
                  }
                  high = 0;
                  low = 0;
               }
      printAdditionTable()
     { 
      case('A') :
      case('a') :  if ((high == 0) || (low == 0))  {       // Stop if we don't have
                      cout << "Error! Invalid range!\n";    // a good range.
                      break;     
                    }                                    
                   cout << "  Addition Table\n";            // Print addition table.
                   for (i = 0; i <= (high - low) + 2; i++)
                     cout << "-----";
                   cout << "\n    ";
                   for (i = low; i <= high; i++)
                     cout << setw(5) << i;
                   cout << "\n";
                   for (i = 0; i <= (high - low) + 2; i++)
                     cout << "-----";
                   cout << "\n";
                    for (i = low; i <= high; i++) {
                      cout << setw(2) << i << " |" ;
                      for (j = low; j <= high; j++)
                        cout << setw(5) << i+j;
                      cout << "\n";
                    }
              }
      printMultipicationTable()
     {         
      case('M') : 
      case('m') :  if ((high == 0) || (low == 0))  {       // Stop if we don't have
                      cout << "Error! Invalid range!\n";    // a good range.
                      break;     
                    }                         
                   cout << "  Multiplication Table\n";     // Print multiplication table.
                   for (i = 0; i <= (high - low) + 2; i++)
                     cout << "------";
                   cout << "\n    ";
                   for (i = low; i <= high; i++)
                     cout << setw(6) << i;
                   cout << "\n";
                   for (i = 0; i <= (high - low) + 2; i++)
                     cout << "------";
                   cout << "\n";
                    for (i = low; i <= high; i++) {
                      cout << setw(2) << i << " |" ;
                      for (j = low; j <= high; j++)
                        cout << setw(6) << i*j;
                      cout << "\n";
                    }
        }
     void quit()
     {   
      case('Q') : 
      case('q') : break;
      default : cout << "Enter \"R\", \"A\", \"M\", or \"Q\"";
    }
  } while ((choice != 'Q') && (choice != 'q'));
} // main()
}
read this:
http://www.cplusplus.com/doc/tutorial/functions/

the way you're 'calling' functions is completely incorrect.

edit:
it also looks like you're trying to use a 'switch' statement (going by your use of the 'case' keyword). Read the last section of this page:
http://www.cplusplus.com/doc/tutorial/control/

Last edited on
Ok, I changed alot of it up but I am getting like 3 bugs that I don't know how to fix.
// This program prints addition and muiltiplication tables for
// the range of numbers entered. The numbers must be positive 
// and the upper value of the range must be greater then the lower 
// value of the range.

#include <iostream>
#include <iomanip>

using namespace std;
void Q = quit
int R = enterRange
int A = printAdditonTable
int m = printMultiplicationTable


int main()
{
   char choice
  int i, j, low = 0, high = 0;
  cout  << setprecision(0);
  do {
    cout << "\n\t\tArithmetic Table Printing\n"   // Print menu.
         << "\tR = Enter Range\n"
         << "\tA = Print Addition Table\n"
         << "\tM = Print Multiplication Table\n"
         << "\tQ = Quit\n\n"
         << "Enter your choice: ";
    cin >> choice;
    cout << "\n\n";
    }
    return (0);
    }
    
    
    
    int enterRange(R)
    {
    int i, j, low = 0, high = 0;
     cout << "Enter low number: ";   // Get high and low numbers.
                  cin  >> low;
                  cout << "Enter high number: ";  
                  cin  >> high;
                  if ((low >= 1) && (high > low)   // If range is good, proceed.
                       && (high < 32))  
                    break;                         // Otherwise, process the error.
                  else if (high <= low) {
                    cout << "Error! High number must be greater than Low number.\n\n";
                  } else if (high <= 1) {
                    cout << "Error! High number must be greater than 1.\n\n";
                   }
                   else if (low <= 0) {
                   cout << "Error! Low number must be greater than 0.\n\n";
                  } else if (high > 31) {
                  }
                  high = 0;
                  low = 0;
               }
               
               
      int printAdditionTable(A)
     { 
     int i, j, low = 0, high = 0;
     if ((high == 0) || (low == 0))  {       // Stop if we don't have
                      cout << "Error! Invalid range!\n";    // a good range.
                      break;     
                    }                                    
                   cout << "  Addition Table\n";            // Print addition table.
                   for (i = 0; i <= (high - low) + 2; i++)
                     cout << "-----";
                   cout << "\n    ";
                   for (i = low; i <= high; i++)
                     cout << setw(5) << i;
                   cout << "\n";
                   for (i = 0; i <= (high - low) + 2; i++)
                     cout << "-----";
                   cout << "\n";
                    for (i = low; i <= high; i++) {
                      cout << setw(2) << i << " |" ;
                      for (j = low; j <= high; j++)
                        cout << setw(5) << i+j;
                      cout << "\n";
                    }
              }
              
              
    int  printMultipicationTable(M)
     {    
     int i, j, low = 0, high = 0;     
       if ((high == 0) || (low == 0))  {       // Stop if we don't have
                      cout << "Error! Invalid range!\n";    // a good range.
                      break;     
                    }                         
                   cout << "  Multiplication Table\n";     // Print multiplication table.
                   for (i = 0; i <= (high - low) + 2; i++)
                     cout << "------";
                   cout << "\n    ";
                   for (i = low; i <= high; i++)
                     cout << setw(6) << i;
                   cout << "\n";
                   for (i = 0; i <= (high - low) + 2; i++)
                     cout << "------";
                   cout << "\n";
                    for (i = low; i <= high; i++) {
                      cout << setw(2) << i << " |" ;
                      for (j = low; j <= high; j++)
                        cout << setw(6) << i*j;
                      cout << "\n";
                    }
        }
        
        
     void quit(Q)
     {   
      default : cout << "Enter \"R\", \"A\", \"M\", or \"Q\"";
    }
  } while ((choice != 'Q') && (choice != 'q'));
} // main()
}
I am getting like 3 bugs that I don't know how to fix



edit: you really need to read that link on functions again.
Last edited on
Example of declaring, defining and calling a function:

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
#include <iostream>

// declaring my function
double someFunction(double x);


int main()
{
	double aNumber = 42.0;

	// call the function
	double myResult = someFunction(aNumber);
	std::cout << aNumber << " doubled is: " << myResult << std::endl;

	return 0;
}


// Completely outside of your main function, define your function
double someFunction(double x)
{
	double returnValue = x * 2.0;
        return returnValue;
}
	
Last edited on
Topic archived. No new replies allowed.