I am currently in a C++ class. We have using Python and my assignment is to do a lab that I have done in python 2.5 in C++, although I have no idea where to begin besides opening Dev C++ or Bloodshed. Could anyone please tell me how I begin the project and use it? This is the lab assignment and what I have for Python. Thanks..

'This lab is simply to make sure you understand the various arithmetic operations. Print the result of doing the following operations: 2.345+1.23, 2.345*1.23, 2.345 – 1.23, 2.345/1.23, 11/4, 11%4. Do them in the order specified, and label your results when you print them.'

Python :
print "2.345 + 1.23 =", 2.345+1.23
print "2.345 * 1.23 =", 2.345*1.23
print "2.345 - 1.23 =", 2.345-1.23
print "2.345 / 1.23 =", 2.345/1.23
print "11 / 4 =", 11/4
print "11 % 4 =", 11%4

Recommended Answers

All 15 Replies

It's very similar in c++

#include <iostream>

int main()
{
  std::cout << "2.345 + 1.23 =" << 2.345+1.23 << '\n';
}

I still am not aware how to begin it though. For example Do I open up Dev C++, New project, Windows Application?, write that in the side, compile and run?

Don't use Dev-C++ -- its and ancient and unsupported compiler/IDE. Much better if you get Code::Blocks which uses current version of MinGW compiler. Here is a tutorial

commented: Agreed. +4

Dev C++ is crap. Don't use it unless you have to. Use Code::Blocks or VC++ Express instead.

I wouldn't use a "Windows Application". I would suggest a "Console Application". Then, you create a *.cpp file and put your code in the *.cpp file.

EDIT:
Oops, AD beat me to it.

I am just using what is available in class on my school pc which I can submit to my instructor

Based on your e-mail address, you seem to be studying at an American Community College. I'd almost bet you have access to something better than Dev-C++. If they teach Python and C++, they probably teach VB and C# as well. Look for Microsoft Visual Studio, that supports C++.

>>... tried that compiled and run and still dont understand...
What don't you understand? This late in the semester you should know at least something about C++ output statements.

You mentioned that you've been using Python. Is this actually a C++ class or some sort of "Intro to Programming"? Or the other possibility, have you been studying properly?

Your good, is it that obvious? haha. Yes I am currently at an American CC waiting to transfer after next semester. I do have that.

I don't understand much of the class as Id like to. I have not been studying as properly as I should either unfortunately. It is an Intro to Computer Science class.

>>I have not been studying as properly as I should either unfortunately. It is an Intro to Computer Science class.
There's the problem. You can't just gloss-over C++ as part of another course, it really needs to be a dedicated course. You have to remain focused because it's such an expansive language with an extremely strict syntax compared to many others. I'm betting you missed a C++ lesson of which how a C++ output statement works was a part.

Here's a tutorial. That should help you get caught up.

>>I have not been studying as properly as I should either unfortunately. It is an Intro to Computer Science class.
There's the problem. You can't just gloss-over C++ as part of another course, it really needs to be a dedicated course. You have to remain focused because it's such an expansive language with an extremely strict syntax compared to many others. I'm betting you missed a C++ lesson of which how a C++ output statement works was a part.

Here's a tutorial. That should help you get caught up.

Thank you. I really appreciate it..

OK I got almost all of it but I am not sure about the % problem.
#include <iostream>
using namespace std;

int main()
{
cout << "2.345 + 1.23 =" << 2.345+1.23 << '\n';
cout << "2.345 * 1.23 =" << 2.345*1.23 << '\n';
cout << "2.345 - 1.23 =" << 2.345-1.23 << '\n';
cout << "2.345 / 1.23 =" << 2.345/1.23 << '\n';
return 0;
}

seems to work

but once I add line 10

#include <iostream>
using namespace std;

int main()
{
cout << "2.345 + 1.23 =" << 2.345+1.23 << '\n';
cout << "2.345 * 1.23 =" << 2.345*1.23 << '\n';
cout << "2.345 - 1.23 =" << 2.345-1.23 << '\n';
cout << "2.345 / 1.23 =" << 2.345/1.23 << '\n';
cout << "2.345 % 1.23 =" << 2.345%1.23 << '\n';
return 0;
}

I get
In function 'int main()':
Line 10: error: invalid operands of types 'double' and 'double' to binary 'operator%'
compilation terminated due to -Wfatal-errors.

% is the "modulo" operator. It returns the remainder of an integer division. It can not operate on floating-point data types (double and float).

This piece of code:

#include <iostream>
using std::cout;

int main() {
  cout << "The remainder of 5 / 3 is: " << 5 % 3 << endl;
  return 0;
}

Will output "The remainder of 5 / 3 is: 2"

EDIT:
Look back at your original Python code. It uses different terms...

Thank you. Is your name Fbody as in camaro/firebird/transams?

Yes, actually. My "Summer Car" is a red/black Convertible T/A WS6.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.