8 Replies - 392 Views - Last Post: 28 January 2013 - 08:31 PM Rate Topic: -----

#1 flamerzade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-January 13

My first (sorta) program in C++

Posted 21 January 2013 - 10:45 PM

Hey this is my first program using C++ any tips to a newbie would be appeciated
#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int sum;
    int rem;

    cout << "division, type in a number \n";
    cin >> a;

    cout << "tpye in another number \n";
    cin >> b;

    rem = a % b;
    sum = a / b;
    cout << "your number is " << sum << " your rem is " << rem << endl;



               return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: My first (sorta) program in C++

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: My first (sorta) program in C++

Posted 21 January 2013 - 10:46 PM

What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 flamerzade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-January 13

Re: My first (sorta) program in C++

Posted 21 January 2013 - 10:47 PM

" your rem is "

}[/code]
[/quote]

rem is remainder

View Postmacosxnerd101, on 21 January 2013 - 10:46 PM, said:

What specific problems or errors are you encountering?

none just showing off my first C++ project :)
Was This Post Helpful? 0
  • +
  • -

#4 MagSharkGG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-January 13

Re: My first (sorta) program in C++

Posted 21 January 2013 - 10:56 PM

nice program keep it up
prob worth at LEAST couple hundred thousand dollars
Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: My first (sorta) program in C++

Posted 21 January 2013 - 10:58 PM

Well, consider your a to be 3 and b to be 2

on execution of line 19, you will get the value of sum to be 1 which is not the correct answer.
So, it would be better if you declare sum as float so that there won't be any truncation of data.

Also, you need to replace your line 19 to be like this.
sum = (float)a / b;


One more thing, your variable names must be logically sounding correct...in your case, sum does nothing even closely related to addition, which it actually means!!

A small suggestion would be to replace the name sum by quo, as it is the quotient you are getting.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,560
  • Joined: 09-June 09

Re: My first (sorta) program in C++

Posted 21 January 2013 - 10:59 PM

Nice job on your first program. I'm not sure if you are aware, but here is a defined floating point variable (single and double precision).
Was This Post Helpful? 0
  • +
  • -

#7 ddeckys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-May 12

Re: My first (sorta) program in C++

Posted 27 January 2013 - 09:08 PM

Try this:
Nice job though.
#include <iostream>

using namespace std;

int main()
{
    int a, b;
    int answer, remainder;

    cout << "Division: type in a number \n";
    cin >> a;

    cout << "Type in another number \n";
    cin >> b;

    remainder = a % b;
    answer = a / b;
    cout << "the answer is " << answer << " and the remainder is " << remainder << endl;
    return 0;
}



View Postraghav.naganathan, on 21 January 2013 - 10:58 PM, said:

Well, consider your a to be 3 and b to be 2

on execution of line 19, you will get the value of sum to be 1 which is not the correct answer.
So, it would be better if you declare sum as float so that there won't be any truncation of data.


I think that his intent was to truncate the data, and then use the "%" operator to find the remainder. Using 3 and 2 in this program would give you 1 remainder 1, which is correct.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4138
  • View blog
  • Posts: 12,875
  • Joined: 25-December 09

Re: My first (sorta) program in C++

Posted 28 January 2013 - 06:49 AM

View Postraghav.naganathan, on 21 January 2013 - 11:58 PM, said:

Well, consider your a to be 3 and b to be 2

on execution of line 19, you will get the value of sum to be 1 which is not the correct answer.
So, it would be better if you declare sum as float so that there won't be any truncation of data.

Also, you need to replace your line 19 to be like this.
sum = (float)a / b;


One more thing, your variable names must be logically sounding correct...in your case, sum does nothing even closely related to addition, which it actually means!!

A small suggestion would be to replace the name sum by quo, as it is the quotient you are getting.

regards,
Raghav


First forcing floating point in the calculation is unwarranted. As already stated by others the purpose here is to find the integral part of the equation, thus the idea is to truncate the data.

Quote

One more thing, your variable names must be logically sounding correct

This is also incorrect, variables can be named almost anything. I do agree however that variable names should be meaningful in the content of the code, but this is not an absolute requirement. I also don't usually recommend using abbreviations, use the actual word instead.

Jim
Was This Post Helpful? 3
  • +
  • -

#9 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: My first (sorta) program in C++

Posted 28 January 2013 - 08:31 PM

Thank you for that information sir, I guess I misread the question and the 'sum' part threw me off as it was a bit confusing.

As for the variable names, what I meant was...it is good programming practice to use the names of variables as logically sounding names, so that when you look at the code after a long time, you still can understand it without any difficulty...:)...also, if any modifications are to be made and the original programmer who wrote the code is not available, it would be helpful for any other programmer to understand the code better if logically sounding variable names are used :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1