Thread: Newbie programmer with his first frustration

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    11

    Newbie programmer with his first frustration

    Hello my first post brings with it my first c++ problem. Having no previous coding experience, I went out and bought C++ Without Fear as recommended by the site and ran into difficulties with the first if/else lesson. It's also the first lesson using the % as a division mechanic. Im using Visual Studio C++ express edition for this.

    Code:
    #include <stdafx.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int  n, remainder;
    
    	cout << "Enter a number and press ENTER: ";
    	cin >> n;
    	remainder = n % 2;
    	if (remainder == 0)
    		cout << "The number is even.";
    	else
    		cout << "The number is odd.";
    
    	return 0;
    }
    Ive looked at the example 5-6 times now and am quite sure that I've copied it verbatim, but this is the first time that I've entered something without it working at all. Any ideas as to what Im doing wrong?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    you can start by telling us what's not working.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    I hit Ctrl + F5 to try to start the program but I get an error message that says there were build errors, and it wont run the program.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are going to have to be more specific. What build errors are you getting?

    The code you posted above is correct, so it is most likely due to a problem with the way your project is set up.

  5. #5
    Emulator
    Join Date
    Feb 2008
    Posts
    43

    Wink Fixed, enjoy!

    Hello, I tested out your code and I found the problem. You need a while statement. I am using while (1), which will keep executing your code. Nice beginner program, never actually thought about making one of those. Also you have good algorithm design for a newbie. Also the only library you need is iostream. It works fine. Here is the refined code...

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int  n, remainder;
        while (1)
        {
    	cout << "Enter a number and press ENTER: ";
    	cin >> n;
    	remainder = n % 2;
    	if (remainder == 0)
    		cout << "The number is even.";
    	else
    		cout << "The number is odd.";
        }
    	return 0;
    }
    Nice program, and keep programming ^^
    Hand over your source code, and nobody gets hurt.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    There is no need for a while statement in that program.

    galactic_ronin: Please post the errors you are getting, be as specific as possible (ie post the full error message). Also mark the lines in the code so we know which error lines up with which line of code. Best of luck.

  7. #7
    Emulator
    Join Date
    Feb 2008
    Posts
    43
    I made it work..What are you talking about? And now it's better.
    Hand over your source code, and nobody gets hurt.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by PЯO View Post
    I made it work..What are you talking about? And now it's better.
    How does it stop, exactly?

    Edit: Although I will give you credit for probably stumbling over the issue, since I'd be willing to bet at least a dime that the problem is the stdafx.h line. Shouldn't that be in quotes?
    Last edited by tabstop; 02-16-2008 at 07:16 PM.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It should be, but Pro didn't stumble upon the issue. The problem is in the project settings (my guess is it's a GUI app and can't find WinMain), and Pro set up a new project where it works. Won't help the OP.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If its finding odd and even this is a much easier way:

    Code:
    if (( number &#37; 2 ) == 0 )
    {
       std::cout << "\nNumber is even!\n";
    }
    
    else
       std::cout << "\nNumber is odd!\n";
    Double Helix STL

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Irrevelant posts have been deleted. If you want to discuss the merits of one IDE over another, or the command line versus a GUI, go do it elsewhere.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by swgh View Post
    If its finding odd and even this is a much easier way:

    Code:
    if (( number % 2 ) == 0 )
    {
       std::cout << "\nNumber is even!\n";
    }
    
    else
       std::cout << "\nNumber is odd!\n";
    You could also optimize it by checking if the lowest bit is set
    Code:
    if (number & 1)
    {
       std::cout << "\nNumber is odd!\n";
    }
    
    else
       std::cout << "\nNumber is even!\n";

  13. #13
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    fair nuff laserlight.
    Last edited by dudeomanodude; 02-18-2008 at 11:36 AM. Reason: sorry
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  14. #14
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I don't want to get off topic again so might I suggest some IDE tutorials on the site?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by mike_g View Post
    You could also optimize it by checking if the lowest bit is set
    Code:
    if (number & 1)
    {
       std::cout << "\nNumber is odd!\n";
    }
    
    else
       std::cout << "\nNumber is even!\n";
    A trap into which I've fallen, too. But you shouldn't do that if there's any chance that the number is negative. Some negative number representations have the bits flipped around.

    Trust the compiler to optimize %2 to &1 if it's safe. I'm pretty sure most of them do it if you compile with optimizations.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie programmer, few quick questions...
    By cjmdjm in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2005, 10:51 PM
  2. Yet another newbie programmer
    By Ken Rogers in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2005, 01:59 PM
  3. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  4. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  5. newbie frustration
    By jediknite in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2003, 05:49 AM