Urgent!!!! Need help with my program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Urgent!!!! Need help with my program

  1. #1
    Join Date
    Mar 2013
    Posts
    2

    Question 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:owerbutton(){
    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;

    }
    }
    }
    Attached Files Attached Files

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Urgent!!!! Need help with my program

    Did you debug your code? Or you hoped we will do it for you?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Urgent!!!! Need help with my program

    Quote Originally Posted by josh223456 View Post
    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

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    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.

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,997

    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)
    Hint: The state of cRadio that is reported isn't always accurate.
    What happens to the class variables when you change mode?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center