I am getting 4 errors when I try to build this program, beginning at the switch statement and am not quite sure what the issue is. Any help would be greatly appreciated.
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
class SplLabVoltmeter
{
double *readings;
double rms;
int sz;
public:
SplLabVoltmeter()
{
readings = NULL;
rms = 0.0;
sz = 0;
}
SplLabVoltmeter(int n)
{
readings = new double[n];
sz = n;
}
void getReadings()
{
cout << "\n Enter Reading of Sine wave ";
for (int i = 0; i<sz; ++i)
{
cout << "\n Enter Reading " << (i + 1) << ": ";
cin >> readings[i];
}
}
void calcRMSVoltage()
{
double sqsum = 0.0;
double mean;
double *t = new double[sz];
for (int i = 0; i<sz; ++i)
{
t[i] = readings[i] * readings[i];
sqsum = sqsum + t[i];
}
mean = sqsum / (double)sz;
rms = sqrt(mean);
}
void showResults()
{
cout << "\n" << "Given Readings of Wave is";
for (int i = 0; i<sz; ++i)
cout << "\t" << readings[i];
cout << "\n RMS Voltage is ....." << setprecision(3) << rms;
}
~SplLabVoltmeter() {};
};
int main()
{
int choice = 0;
int r = 0;
while (choice != 4)
{
cout << "\n Which Wave 1. Sine 2. Traingular 3. Square Wave 4. Exit \n Enter Choice:";
cin >> choice;
switch (choice)
{
case 1:
cout << "\n How many Readings for Sine wave do you give : ";
cin >> r;
SplLabVoltmeter obj(r);
obj.getReadings();
obj.calcRMSVoltage();
cout << "\n For Sine Wave...\n";
obj.showResults();
break;
case 2:
cout << "\n How many Readings for Triangular wave do you give : ";
cin >> r;
SplLabVoltmeter obj1(r);
obj1.getReadings();
obj1.calcRMSVoltage();
cout << "\n For Triangular Wave...\n";
obj1.showResults();
break;
case 3:
cout << "\n How many Readings for Square wave do you give : ";
use cstdio and cmath instead of the C .h versions.
the problem here is subtle, you need to put {} around the cases:
case 1:
{//here
code;
}//and here
break; //outside
case 2: //repeat pattern
and consider a default case.
the problem you see, by the way, is caused by creation of variables in the cases. The compiler can't infer the scope correctly, the braces force the scope and tell it what to do explicitly.