1 Attachment(s)
Urgent!!!! Need help with my program
I am making a radio program for one of my classes. It has to have a working clock, go from am, fm, aux, to mp3. AM frequency (530 kHz-1610kHz, going up by 10kHz), FM frequency (88.1Mhz-180Mhz, going up by 100kHz). I keep getting a weird number for my volume when I turn the radio on if you can fix this it would be very greatly appreciated. And my frequency wont work. PLS fix!!!!!!!!!!
#include <stdio.h>
#include <stdlib.h> //All the file librabries that are needed for this assignment
#include <conio.h>
#include <iostream>
#include <ctype.h>
class cRadio{
public:
cRadio();
~cRadio();
enum inputmodes {MP3,AM,FM,AUX};
void powerbutton();
void volumeup();
void volumedown();
void antenna();
void setmode();
void mutevol();
void change();
void frequp();
void freqdown();
private:
bool power;
short volume, mute;
inputmodes mode;
int freq;
};
cRadio::cRadio(){
volume=0;
mute=0;
power=false;
mode=AUX;
}
cRadio::~cRadio(){
}
void cRadio::powerbutton(){
if(power==true){
printf("Radio is off\n");
power=false;
}
else if(power==false){
printf("Radio is on\n");
printf("Volume = %d\n");
if (mode<=AUX){
if (mode==AM){
printf("Input Mode: AM\n",mode);
}else
if (mode==FM){
printf("Input Mode: FM\n",mode);
}else
if (mode==MP3){
printf("Input Mode: MP3\n",mode);
}else
if (mode==AUX){
printf("Input Mode: AUX\n",mode);
}
}
power=true;
}
}
void cRadio::volumeup(){
if (power==false){
}else
if (mute!=0){
}else
if (volume<50){
volume++;
printf("Volume = %d\n", volume);
}else
if (volume==50){
printf("Volume = 50\n");
}
}
void cRadio::volumedown(){
if(power==false){
}else
if (mute!=0){
}else
if (volume>0){
volume--;
printf("Volume = %d\n", volume);
}else
if (volume==0){
printf("Volume = 0\n");
}
}
void cRadio::mutevol(){
if (power==false){
}else
if(mute==0){
mute=volume;
volume=volume-mute;
printf("Volume = mute\n");
}else
if(mute!=0){
volume=mute;
printf("Volume = %d\n", volume);
mute=0;
}
}
void cRadio::frequp(){
if (power==false){
}
else if (mode==FM){
if(freq<108000000){
freq=freq+100000;
printf("Frequency = %d Hz\n",freq);
}
else if (freq==108000000){
freq=freq-19900000;
printf("Frequency = %d Hz\n",freq);
}
}
else if (mode==AM){
if(freq<1610000){
freq=freq+10000;
printf("Frequency = %d Hz\n",freq);
}
else if (freq==1610000){
freq=freq-1080000;
printf("Frequency = %d Hz\n",freq);
}
}
}
void cRadio::freqdown(){
if (power==false){
}
else if (mode==FM){
if(freq>88100000){
freq=freq-100000;
printf("Frequency = %d Hz\n", freq);
}
else if (freq==88100000){
freq=freq+19900000;
printf("Frequency = %d Hz\n", freq);
}
}
else if(mode==AM){
if(freq>530000){
freq=freq-10000;
printf("Frequency = %d Hz\n", freq);
}
else if (freq==530000){
freq=freq+1080000;
printf("Frequency = %d Hz\n", freq);
}
}
}
void cRadio::setmode(){
if(power==false){
}
else if (mode<AUX){
mode = static_cast<inputmodes>(mode + 1);
if(mode==MP3){
printf("Input Mode: MP3\n", mode);
}
else if(mode==AM){
printf("Input Mode: AM\n", mode);
}
else if (mode==FM){
printf("Input Mode: FM\n",mode);
}
else if (mode==AUX){
printf("Input mode: AUX\n",mode);
}
}
else if (mode==AUX){
mode=MP3;
printf("Input mode: MP3\n");
}
}
void main (){
cRadio rad;
while(true){
switch(tolower(_getch())){
case ' ':
rad.powerbutton();
break;
case 'i':
rad.setmode();
break;
case 'u':
rad.volumeup();
break;
case 'd':
rad.volumedown();
break;
case 'm':
rad.mutevol();
break;
case 'g':
rad.frequp();
break;
case 'h':
rad.freqdown();
break;
}
}
}
Re: Urgent!!!! Need help with my program
Did you debug your code? Or you hoped we will do it for you?
Re: Urgent!!!! Need help with my program
Quote:
Originally Posted by
josh223456
I am making a radio program for one of my classes. It has to have a working clock, go from am, fm, aux, to mp3. AM frequency (530 kHz-1610kHz, going up by 10kHz), FM frequency (88.1Mhz-180Mhz, going up by 100kHz). I keep getting a weird number for my volume when I turn the radio on if you can fix this it would be very greatly appreciated. And my frequency wont work. PLS fix!!!!!!!!!!
1) Use code tags when posting code, as it is unreadable.
2) So you want us to
a) copy your code,
b) compile your code,
c) step through your code with the debugger,
d) get back to you with what to fix.
Is that right? Well, that isn't going to happen. It's your job to do all of those steps, not ours. Where is your effort in debugging the code, as Victor pointed out?
Regards,
Paul McKenzie
Re: Urgent!!!! Need help with my program
While reformatting your code to make it readable I saw some things that make me belive your code is perfectly alright except for a few mistakes that you will easily detect while running it in the debugger.
Hint: The state of cRadio that is reported isn't always accurate.
Re: Urgent!!!! Need help with my program
That code wouldn't compile for me, but without proper formatting, it's too hard to read to figure out why not.
Re: Urgent!!!! Need help with my program
Without giving you the answers for your assignment, a couple of points
1) Formatted code with proper indentation is much easier to read than unformatted code and helps reduces the possibility of errors re blocks.
2) For ANSI c++ compatability, the main function should return an int. This is 0 if the program terminated normally otherwise an error code.
3) It's not helpful when your program is used to be just presented with a blinking cursor and no idea as to what is expected. Some instructions shown on the screen at the start would be useful.
4) You use the switch statement in your main function, yet in the class functions you have multiple nested if statements. Personally, I would favour replacing these nested if statements with switch statements. The switch control variable can be of type enum.
5) The class constructor(s) should initialise all class variables so that their initial value is always known.
6) Rather than setting class variables using assignment as in
Code:
cRadio::cRadio()
{
volume = 0;
mute = 0;
power = false;
mode = AUX;
}
Good practice is to use initialisers as in
Code:
cRadio::cRadio()
: power(false), volume(0), mute(0), mode(AUX)
{
}
7)
Quote:
Hint: The state of cRadio that is reported isn't always accurate.
What happens to the class variables when you change mode?