Help if basic question

This is a discussion on Help if basic question within the C++ Programming forums, part of the General Programming Boards category; Ok I'm just going over the basic trying to sharping my skills. I was looking over this program and I ...

  1. #1
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    Help if basic question

    Ok I'm just going over the basic trying to sharping my skills. I was looking over this program and I was reading the "How It Works " section telling me I should add the break so the loop doesn't keep running and stuff like that. Then it said instead of wright the code like this
    Code:
    int is_prime;
    
    is_prime = true;
    I should wright it like this
    Code:
    int is_prime = true
    and I was jw as you can see below should I also do the same for
    Code:
    is_prime = false;
    thanks. sorry for this crappy question.

    Code:
    #include <iostream>
    #include <cmath>
    
    int main ( ) {
    
    	int		n;
    	int		i;
    
    	int            is_prime = true;
    
    	std::cout << "Enter a number and press ENTER: ";
    	std::cin >> n;
    
    	i = 2;
    
    	while ( i <= sqrt ( double ( n ) ) ) {
    
    		if ( n % i == 0 ) {
    
    
    			is_prime = false;
    			break;
    
    		}
    
    		i++;
    
    	}
    
    	if ( is_prime ) {
    		
    		std::cout << "Number is prime. ";
    
    	} else {
    
    		std::cout << "Number is not prime. ";
    	}
    	
    	std::cout << std::endl;
    
    	return ( 0 );
    
    }
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Code:
    Short answer: no.
    
    Better answer:
    Since you are using is_prime as a boolean-valued variable (only true or false), declare it
    as type bool instead of int.
    
    When you type
    bool is_prime;
    or
    int is_prime;
    you are declaring the variable (allocating memory space to hold a value of the
    specified type) and giving it a name.
    
    When you type
    is_prime = true;
    you are assigning a specific value to be stored at that location.
    
    When you type
    bool is_prime = true;
    you are simply combining those two steps.
    Thereafter, since the variable is_prime already exists, whenever you want to assign a
    value to that same variable, just use the assignment command.
    
    In your example, if you wrote
    bool is_prime = false;
    inside that while loop, you would actually be declaring (and assigning as "false") a
    completely new variable named "is_prime" which only "exists" inside the {} block of that
    loop, and is completely independent of and has no effect on the other "is_prime"
    variable that you declared at the beginning of your main function.

  3. #3
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    Cool

    Thanks man. You couldn't of explained it any better.
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Btw, wright is spelled write. Write, ie type.
    And when reserving space for something... then basically, it's a definition.
    When you create a variable, you are basically both declaring it and defining it.
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Btw, wright is spelled write. Write, ie type.
    In this particular case yes. There are three homophones:
    wright, write and right all sound the same, but mean different things:
    Wright is something you may do to a wheel to make is straight/round.
    Write is something you do with a pen or a keyboard when producing text.
    Right is the opposite of either left or wrong, depending on context.

    Thanks to the rich English language and it's inconsistent/variable spelling.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    In this particular case yes. There are three homophones:
    wright, write and right all sound the same, but mean different things:
    Wright is something you may do to a wheel to make is straight/round.
    Write is something you do with a pen or a keyboard when producing text.
    Right is the opposite of either left or wrong, depending on context.

    Thanks to the rich English language and it's inconsistent/variable spelling.

    --
    Mats
    I never knew "wright" was a real word. I've never seen it used before.
    You left one out though: rite
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Hehehe... gotta love english, eh?
    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
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    I would just like to add that a person can also be named Wright.

    ...and right can also mean "is it not so?"

    This should clear things up.
    home = hat.position;

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by DrSnuggles View Post
    This should clear things up.
    Right can also mean wrong, as in "yeah ... right".

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by R.Stiltskin View Post
    Right can also mean wrong, as in "yeah ... right".
    Yes, that's called sarcasm, and is not unique to the English language, nor is it really good to use in forums - unless it is VERY clear that's what you are doing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 03:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21