beginners question

This is a discussion on beginners question within the C++ Programming forums, part of the General Programming Boards category; I'm doing some practice problems in my c++ book and this particular one has me stuck. The problem asks to ...

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

    beginners question

    I'm doing some practice problems in my c++ book and this particular one has me stuck. The problem asks to calculate 80% of the amount it would cost to replace the structure but when I try to compile it says "error C2296: '%' : illegal, left operand has type 'float'" on the 11th line. What should I do differently?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	float house, replacement;
    
    	cout << "Please enter the replacement costs of any of your buildings: ";
    		cin >> house;
    			replacement = house % 80;
    	cout << "The minimum amount of insurance you should get is " << replacement;
    return 0;
    }

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    In C (C++), % is the modulo operator. Works only on integer.

    What you are looking for is
    Code:
    replacement = house * 0.80f

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    22
    It worked, thanks.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    THe % operator is probably not what you want in this case - I'm assuming you want to calculate 80% of the house value.

    The % operator in C and C++ is a modulo operator - modulo gives you the remainder[1] in an integer division, e.g. 5 % 3 will give 2.

    [1] Not EXACTLY remainder, because negative numbers are not treated as the mathematician would do.
    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.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by c++prog View Post
    I'm doing some practice problems in my c++ book and this particular one has me stuck. The problem asks to calculate 80% of the amount it would cost to replace the structure but when I try to compile it says "error C2296: '%' : illegal, left operand has type 'float'" on the 11th line. What should I do differently?
    Remember your math lessons!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's Question on QT
    By unix7777 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2008, 05:53 PM
  2. beginner's question :D
    By kingliaho in forum C Programming
    Replies: 5
    Last Post: 10-17-2008, 06:20 PM
  3. beginner's question about C
    By Roberto Llovera in forum C Programming
    Replies: 5
    Last Post: 02-26-2004, 05:14 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM

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