error: invalid operands of types ‘float’ and ‘float’ to binary ‘operator%’

This error is because “operator%” is only for Integers so it won’t work for Float

Instead of using operator%” for Float use fmod() function from math Lib

fmod

<cmath>
     double fmod (      double numerator,      double denominator );
      float fmod (       float numerator,       float denominator );
long double fmod ( long double numerator, long double denominator );
Compute remainder of division

Returns the floating-point remainder of numerator/denominator.

The remainder of a division operation is the result of subtracting the integral quotient multiplied by the denominator from the numerator:

remainder = numerator – quotient * denominator

Parameters

numerator
Floating point value with the division numerator.
denominator
Floating point value with the division denominator.

 

Return Value

The remainder of dividing the arguments.

Portability

In C, only the double version of this function exists with this name.

Example


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

/* fmod example */
#include<conio.h>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
    float x=8.8, y=3.5, z=-5.2, a, b, c, d, e, f, g, h;
    a=x+y+z;
    b=2*y+3*(x-z);
    c=x/y;
    //d = (float)x%y; //This Won't work for FLOAT or using fmod()
    d = fmod(x,y);
    e=x/(y+x);
    f=(x/y)+z;
    g=2*x/3*y;
    h=2*x/(3*y);
    printf("Answer a:%f\n",a);
    printf("Answer b:%f\n",b);
    printf("Answer c:%f\n",c);
    printf("Answer d:%f\n",d);
    printf("Answer e:%f\n",e);
    printf("Answer f:%f\n",f);
    printf("Answer g:%f\n",g);
    printf("Answer h:%f\n",h);
    getch();
    return 0;
}

Output:

Answer a:7.100000
Answer b:49.000000
Answer c:2.514286
Answer d:1.800000
Answer e:0.715447
Answer f:-2.685714
Answer g:20.533333
Answer h:1.676190
By cplusplus on February 21, 2013 | Daily C++ Blog Posts | A comment?

Using Keyboard Hit

I found this Question on Net, Just as a reply i  here and sharing this.

“How can we interrupt or terminate getchar() function before pressing Enter key ?”

How can i use Getchar() but not wait until users Press a Key.

How can i do this Please Help.

 

 

By cplusplus on February 20, 2013 | Daily C++ Blog Posts | 2 comments

Hungarian Notation Disadvantages

Hungarian Notation is a Bad Patrice ? There always been a confusion about Hungarian Notation is Good or Bad.
Some people tell using Hungarian Notation is really a Bad practice. They say Hungarian Notation Disadvantages are :

Most arguments against Hungarian Notation are against Systems Hungarian Notation, not Apps Hungarian Notation. Some potential issues are:

  • The Hungarian Notation is redundant when type-checking is done by the compiler. Compilers for languages providing type-checking ensure the usage of a variable is consistent with its type automatically; checks by eye are redundant and subject to human error.
  • All modern integrated development environments display variable types on demand, and automatically flag operations which use incompatible types, making the notation largely obsolete.
  • Hungarian Notation becomes confusing when it is used to represent several properties, as in a_crszkvc30LastNameCol: a constant reference argument, holding the contents of a database column LastName of type varchar(30) which is part of the table’s primary key.
  • It may lead to inconsistency when code is modified or ported. If a variable’s type is changed, either the decoration on the name of the variable will be inconsistent with the new type, or the variable’s name must be changed. A particularly well known example is the standard WPARAM type, and the accompanying wParam formal parameter in many Windows system function declarations. The ‘w’ stands for ‘word’, where ‘word’ is the native word size of the platform’s hardware architecture. It was originally a 16 bit type on 16-bit word architectures, but was changed to a 32-bit on 32-bit word architectures, or 64-bit type on 64-bit word architectures in later versions of the operating system while retaining its original name (its true underlying type is UINT_PTR, that is, an unsigned integer large enough to hold a pointer). The semantic impedance, and hence programmer confusion and inconsistency from platform-to-platform, is on the assumption that ‘w’ stands for 16-bit in those different environments.
  • Most of the time, knowing the use of a variable implies knowing its type. Furthermore, if the usage of a variable is not known, it cannot be deduced from its type.
  • Hungarian Notation strongly reduces the benefits of using feature-rich code editors that support completion on variable names, for the programmer has to input the whole type specifier first.
  • It makes code less readable, by obfuscating the purpose of the variable with needless type and scoping prefixes.[2]
  • The additional type information can insufficiently replace more descriptive names. E.g. sDatabase does not tell the reader what it is. databaseName might be a more descriptive name.
  • When names are sufficiently descriptive, the additional type information can be redundant. E.g. firstName is most likely a string. So naming it sFirstName only adds clutter to the code.
  • It’s harder to remember the names.

What to know about Hungarian Notation Advantages

By cplusplus on | Daily C++ Blog Posts | 1 comment

Hungarian Notation Advantages

Hungarian Notation is a Good Patrice ?
There always been a confusion about Hungarian Notation is Good or Bad.Some people tell using Hungarian Notation is really a Good practice. They say Hungarian Notation Advantages are :We can tell the variable type but seeing the variable name. (With out Hungarian Notation Its hard to tell the variable type when the code is outside IDE like Docs, Printouts etc)
  • The type of value returned by a function is determined without lookup (i.e. searching for function definitions in other files, e.g. “.h” header files, etc.)
  • The formatting of variable names may simplify some aspects of code refactoring (while making some aspects more error-prone).
  • Multiple variables with similar semantics can be used in a block of code: dwWidth, iWidth, fWidth, dWidth
  • Variable names can be easy to remember from knowing just their types.
  • It leads to more consistent variable names
  • Inappropriate type casting and operations using incompatible types can be detected easily while reading code
  • Useful with string-based languages where numerics are strings (Tcl for example)
  • In Apps Hungarian, the variable name guards against using it in an improper operation with the same data type by making the error obvious as in: heightWindow = window.getWidth()
  • When programming in a language that uses dynamic typing or that is completely untyped, the decorations that refer to types cease to be redundant. Such languages typically do not include declarations of types (or make them optional), so the only sources of what types are allowed are the names themselves, documentation such as comments, and by reading the code to understand what it does. In these languages, including an indication of the type of a variable may aid the programmer. As mentioned above, Hungarian Notation expanded in such a language (BCPL).
  • In complex programs with lots of global objects (VB/Delphi Forms), having a basic prefix notation can ease the work of finding the component inside of the editor. Typing btn and pressing <Ctrl-Space> causes the editor to pop up a list of Button objects.
  • Applying Hungarian Notation in a narrower way, such as applying only for member variables helps avoiding naming collision.

What to know about Hungarian Notation Disadvantages

By cplusplus on | Daily C++ Blog Posts | 2 comments

Random numbers question

I need to create a cpp that picks multiple random numbers that do not repeat. I can already generate numbers but how do I make the cpp exclude the already picked numbers?

By cplusplus on February 18, 2013 | Public C++ Questions | 2 comments

what is the best IDE for C++

When I started Programming C++,  i was confused about What C++ IDE I should use. Are you looking for Best C++ IDE like me. After years of experience trying different C++ IDE I Picked Top 5 C++ IDE and More all are FREE C++ IDEs.

Hope you guys agree with this list. If Not leave a comment.

Eclipse

Eclipse is the Best of all C++ IDE (Eclipse also support other Programing Languages like Java, PHP etc. using addon). The Eclipse is an advanced C and C++ IDE based on the popular Eclipse platform. Eclipse features various source knowledge tools, managed builds, such as type hierarchy, call graph, include browser, macro definition browser, code editor with syntax highlighting, source code refactoring, code generation, visual debugging tools, including memory, registers, and disassemble viewers.

Netbeans IDE

You’ll be probably surprised to see Netbeans listed here, as its a popular Java IDE. But it can be used for C and C++ development as well. You can develop professional quality native applications in C, C++ using Netbeans IDE.

Its features include C and C++ unit testing, integrated profiler, packaging support, gdb integration, syntax highlighting and code assistance.

Code::Blocks

Code::Blocks or Codeblocks is a free C++ IDE which can be used even in the most demanding situations. It is designed from the scratch, to be very extensible and fully configurable.

It features customizable and extensible syntax highlighting, code folding, tabbed interface, code completion, class Browser, multi user support and much more.

Code::Blocks also have a consistent look, feel and operation across all platforms, including linux and Windows.

Code::Blocks is built around a plugin framework so that it can be easily extended with hundreds of plugin developed by various third party developers.

Bloodshed Dev-C++

Bloodshed Dev-C++ or DevCPP in short, is a full-featured IDE for C and C++ programming languages. It uses Mingw port of GCC (GNU Compiler Collection) as its compiler. Its features includes support for GCC-based compilers, integrated debugging using gdb, customizable syntax highlighting editor, automatic code completion, Profiling support, and much more.

Microsoft Visual Studio Express

Microsoft® Visual Studio® 2010 Express is probably the best C++ IDE. Its free but you have to register your copy within 30 days of installation.

By cplusplus on February 16, 2013 | Daily C++ Blog Posts | 3 comments

Code For C++ Program

Hi Everyone,

Here you can find “Code For C++ Program” that is Sample C++ Codes. They can be New, Old, Samples of C++ Programing for your New Application Development Or Learning C++ with C++ Sample  from this “Code For C++ Program“.

You guys can use these C++ samples  from “Code For C++ Program” as you like (Use on your own risk).

If you have any good C++ Samples you are always welcome to share here. Please contact me at

sampleCPPcodes@cplusplustutorials.com

 

If you want any sample code which is not here Please Leave a Request at Comment and You will get your Required Find your “Code For C++ Program”

This is all about “Code For C++ Program“. Enjoy Guys. (and Let me know what you think :-) )

By cplusplus on February 15, 2013 | Daily C++ Blog Posts | A comment?

Which Programing Language is Best for Beginners

What Programing Language is Best for Beginners,

what do you Think ?

 

Best Programming Language for Beginners

By cplusplus on January 31, 2013 | Entertaining | A comment?

How to print **** while taking in password in C/C++ ?

I am a beginner of c programing and am trying to do some often met c programs. I want to take in password, so i dont want it to be printed even while scanning, so not able to use clrscrn. Please help me in this regard by givving me a program corressponding.

By cplusplus on January 28, 2013 | Public C++ Questions | 6 comments

4.7 — Structs

There are many instances in programming where we need more than one variable in order to represent something. For example, to represent yourself, you might want to store your name, your birthday, your height, your weight, or any other number of characteristics about yourself. You could do so like this:

1
2
3
4
5
6
char strName[20];
int nBirthYear;
int nBirthMonth;
int nBirthDay;
int nHeight; // in inches
int nWeight; // in pounds

However, you now have 6 independent variables that are not grouped in any way. If you wanted to pass information about yourself to a function, you’d have to pass each variable individually. Furthermore, if you wanted to store information about more people, you’d have to declare 6 more variables for each additional person! As you can see, this can quickly get out of control.

Fortunately, C++ allows us to create our own user-defined aggregate data types. An aggregate data type is a data type that groups multiple individual variables together. One of the simplest aggregate data type is the struct. A struct (short for structure) allows us to group variables of mixed data types together into a single unit.

Because structs are user-defined, we first have to tell the compiler what our struct looks like before we can begin using it. To do this, we declare our struct using the struct keyword. Here is an example of a struct declaration:

1
2
3
4
5
6
struct Employee
{
    int nID;
    int nAge;
    float fWage;
};

This tells the compiler that we are defining a struct named Employee. The Employee struct contains 3 variables inside of it: two ints and a float. These variables are called members (or fields). Keep in mind that the above is just a declaration — even though we are telling the compiler that the struct will have variables, no memory is allocated at this time.

In order to use the Employee struct, we simply declare a variable of type Employee:

1
Employee sJoe;

sJoe is a variable of type Employee. As with normal variables, declaring a variable allocates memory for that variable. Typically, the size of a struct is the sum of the size of all it’s members. In this case, since each integer is 4 bytes and a float is 4 bytes, Employee would be 12 bytes. However, some platforms have specific rules about how variables must be laid out in memory — consequently, the compiler may leave gaps between the variables. As a result, we can say the struct will be at minimum 12 bytes.

To find out the exact size of Employee, we can use the sizeof operator:

1
cout << "The size of Employee is " << sizeof(Employee);

On the author’s Pentium 4, this prints The size of Employee is 12.

When we declare a variable such as Employee sJoe, sJoe refers to the entire struct (which contains the member variables). In order to access the individual members, we use the member selection operator (which is a period). As with normal variables, struct member variables are not initialized, and will typically contain junk. We must initialize them manually. Here is an example of using the member selection operator to initialize each member variable:

1
2
3
4
Employee sJoe;
sJoe.nID= 14; // initialize nID within sJoe
sJoe.nAge = 32; // initialize nAge within sJoe
sJoe.fWage = 24.15; // initialize fWage within sJoe

It is possible to declare multiple variables of the same struct type:

1
2
3
4
5
6
7
8
9
Employee sJoe; // create an Employee struct for Joe
sJoe.nID = 14;
sJoe.nAge = 32;
sJoe.fWage = 24.15;
Employee sFrank; // create an Employee struct for Frank
sFrank.nID = 15;
sFrank.nAge = 28;
sFrank.fWage = 18.27;

In the above example, it is very easy to tell which member variables belong to Joe and which belong to Frank. This provides a much higher level of organization than individual variables would. Furthermore, because the members all have the same name, this provides consistency across multiple variables of the same type.

Struct member variables act just like normal variables, so it is possible to do normal operations on them:

1
2
3
4
5
6
7
8
9
10
int nTotalAge = sJoe.nAge + sFrank.nAge;
if (sJoe.fWage > sFrank.fWage)
    cout << "Joe makes more than Frank" << endl;
// Frank got a promotion
sFrank.fWage += 2.50;
// Today is Joe's birthday
sJoe.nAge++;

Another big advantage of using structs over individual variables is that we can pass the entire struct to a function that needs to work with the members:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
void PrintInformation(Employee sEmployee)
{
    using namespace std;
    cout << "ID:   " << sEmployee.nID << endl;
    cout << "Age:  " << sEmployee.nAge << endl;
    cout << "Wage: " << sEmployee.fWage << endl << endl;
}
int main()
{
    Employee sJoe; // create an Employee struct for Joe
    sJoe.nID = 14;
    sJoe.nAge = 32;
    sJoe.fWage = 24.15;
    Employee sFrank; // create an Employee struct for Frank
    sFrank.nID = 15;
    sFrank.nAge = 28;
    sFrank.fWage = 18.27;
    // Print Joe's information
    PrintInformation(sJoe);
    // Print Frank's information
    PrintInformation(sFrank);
    return 0;
}

In the above example, we pass an entire Employee struct to PrintInformation(). This prevents us from having to pass each variable individually. Furthermore, if we ever decide to add new members to our Employee struct, we will not have to change the function declaration or function call!

PrintInformation() uses the Employee struct passed to it to print out employee information to the screen. The above program outputs:

ID:   14
Age:  32
Wage: 24.15

ID:   15
Age:  28
Wage: 18.27

Structs can contain other structs. For example:

1
2
3
4
5
6
7
struct Company
{
    Employee sCEO; // Employee is a struct within the Company struct
    int nNumberOfEmployees;
};
Company sMyCompany;

In this case, if we wanted to know what the CEO’s salary was, we simply use the member selection operator twice: sMyCompany.sCEO.fWage;

This selects the sCEO member from sMyCompany, and then selects the fWage member from within sCEO.

Initializer lists

Initializing structs member by member is a little cumbersome, so C++ supports a faster way to initialize structs using an initializer list. This allows you to initialize some or all the members of a struct at declaration time.

1
2
3
4
5
6
7
8
struct Employee
{
    int nID;
    int nAge;
    float fWage;
};
Employee sJoe = {1, 42, 60000.0f}; // nID=1, nAge=42, fWage=60000.0

You can use nested initializer lists for nested structs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Employee
{
    int nID;
    int nAge;
    float fWage;
};
struct Company
{
    Employee sCEO; // Employee is a struct within the Company struct
    int nNumberOfEmployees;
};
Company sCo1 = {{1, 42, 60000.0f}, 5};

A few final notes on structs

The “m_” Hungarian Notation prefix for members is typically not used for structs, even though structs contain members. This is (in part) because all variables in a struct are members! Consequently, labeling them with a “m_” prefix is somewhat redundant.

It is common to declare structs in a header file, so they can be accessed by multiple source files.

The class aggregate data type builds on top of the functionality offered by structs. Classes are at the heart of C++ object-oriented programming. Understanding structs is the first step towards object-oriented programming!

Quiz

1) You are running a website, and you are trying to keep track of how much money you make per day from advertising. Declare an advertising struct that keeps track of how many ads you’ve shown to readers, what percentage of users clicked on ads (as a floating point number between 0 and 1), and how much you earned on average from each ad that was clicked. Read in values for each of these fields from the user. Pass the advertising struct to a function that prints each of the values, and then calculates how much you made for that day (multiply all 3 fields together).

2) Write a struct to hold a fraction. The struct should have a integer numerator and a integer denominator member. Declare 2 fraction variables and read them in from the user. Write a function called multiply that takes both fractions, multiplies them together, and prints the result out as a decimal number.

By cplusplus on January 26, 2013 | C++ Tutorials for Beginners | A comment?

C++ Questions
C++ Tutorials for Beginners
Daily C++ Blog Posts
Entertaining
General
Public C++ Questions
Questions