Thread: beep when even?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    beep when even?

    I need help with this
    Write a program that will get a number from user and print ""Hello world" that time. But remember if the entered number is even then the program should add 1 to that value and print the value that times and your program should only print the output odd times.
    E.g. If user entered 2 or any even value then add 1 to that value and print the string that times and also show a beep at last output if and only if the entered value is even.


    my code
    Code:
    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    int num;
    clrscr();
    cout<<"\n How many times do you want to print Hello World ?  ";
    cin>>num;
    for(int i=1;i<=num;i++)
    	{
    	if(num%2==0)
    	num+=1;     //this will increment if num is even
    	cout<<"\n Hello World ";
    	}
             cout<<"\a";
    getch();
    }
    I cant do the beep part if the num is even. Though it does gives u beep at the end wether if its even or odd...

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's exactly what you tell it to do. The compiler cannot read your mind.

    It is also strange why you should check and increment num within the loop.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You severely need to learn indentation: https://apps.sourceforge.net/mediawi...le=Indentation
    Then you need to get rid of void main: http://apps.sourceforge.net/mediawik...itle=Void_main
    Then you need to build a flowchart as what you want to do and then translate it to code.
    You could even translate your current code to a flowchart as an exercise to see what is wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    i do use int main too but i am used to void main though il switch to int main from now on

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You severely need to learn indentation:
    Ahmed,

    But, it's not the indentation that's causing your bug. It's the placement of the curly braces.

    You have a pair of braces following your if-statement. Everything inside the braces depends on the if-condition. The "beep" code is not inside the braces, so it gets executed every time through the loop, regardless of the if-condition.
    Last edited by DougDbug; 11-06-2008 at 06:52 PM.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    but when i but it in the loop body , the beep sounds as many times u want to repeat hello world...

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And that is why you need to make a flowchart. This is a logic error, one we should not have to help you out with if you wish to be a programmer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Code:
    <iostream.h> // old as the hills - use <iostream>
    
    <conio.h> // do not even need it - and its also non-standard
    
    getch() // replace it with std::cin.get();
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linux beep function
    By Calef13 in forum C++ Programming
    Replies: 7
    Last Post: 07-14-2007, 01:39 AM
  2. I need to play a pre recorded vox file using C
    By m.sudhakar in forum C Programming
    Replies: 4
    Last Post: 11-17-2006, 06:59 PM
  3. alert beep sound
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 02-14-2006, 02:24 PM
  4. Beep();
    By SirCrono6 in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2004, 04:47 PM
  5. Something Like beep()
    By Trauts in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2003, 02:51 PM