943,913 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2423
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 27th, 2008
0

Please help i am new in c++ programming

Expand Post »
Write a program which will calculate, for every integer that is being input, a code in the following way

first digit multiplied by 3
plus
second digit multiplied with the third digit
minus
fourth digit

thus the number 3124(of type int) should generatethe code 7, because 3*3 +1*2 - 4=7.
you may assume that the given integers will all consist of four digits. submit printouts of the program and output with he six integers below as input. use a for loop iterating from 1 to 6. NB: You may not input the digits separately, but should input the number as a whole. (hint:Use / and % in the calculation.)

3255
1067
1393
2892
1111
2020

i tried this program below but it gives me (-2answer)
please help me
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main( )
  5. //Initialize integers
  6. {
  7. int number,first,second,third,fourth,answer;
  8.  
  9. //For loop
  10. for(int x=1;x <= 6;x++)
  11.  
  12. //Input of number from user
  13. cout << "Enter in number: ";
  14. cin >> number,first,second,third,fourth;
  15.  
  16. //Get the first digit in the thousands place
  17. first = number / 1000;
  18. number = number % 1000;
  19.  
  20. //Get the second digit in the hundreds place
  21. second = number / 100;
  22. number = number % 100;
  23.  
  24. //Get the third digit in the tens place
  25. third = number / 10;
  26. number = number % 10;
  27.  
  28. //Remaining is the ones place
  29. fourth = number;
  30.  
  31. //Calculate and give answer
  32. answer = first *3+second *third -fourth ;
  33. cout << "Your aswer is" << answer;
  34. cin >> answer;
  35. return 0;
  36.  
  37. }
Last edited by Ancient Dragon; Apr 27th, 2008 at 6:02 pm. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
loeto is offline Offline
14 posts
since Apr 2008
Apr 27th, 2008
0

Re: Please help i am new in c++ programming

Its not
C++ Syntax (Toggle Plain Text)
  1. cin >> number,first,second,third,fourth;

but
C++ Syntax (Toggle Plain Text)
  1. cin >> number>>first>>second>>third>>fourth;

and kindly use code tags in future
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
Apr 27th, 2008
0

Re: Please help i am new in c++ programming

it looks like you are prompting for 6digits and you specified that the requirement is to get a whole number...then change your input to get the number as a whole
C++ Syntax (Toggle Plain Text)
  1. cin>>number;

your for loop doesnt look controlled, why are you prompting the user for "answer"??
Last edited by joshmo; Apr 27th, 2008 at 2:42 pm.
Reputation Points: 19
Solved Threads: 20
Posting Whiz in Training
joshmo is offline Offline
280 posts
since Oct 2007
Apr 27th, 2008
0

Re: Please help i am new in c++ programming

Thanx for that correction
But still the problem is not solved the program gives me the wrong output. i need help on the calculation formular.

GUYS I NEED YOUR HELP
Reputation Points: 10
Solved Threads: 0
Newbie Poster
loeto is offline Offline
14 posts
since Apr 2008
Apr 27th, 2008
0

Re: Please help i am new in c++ programming

Thanx for that correction
But still the problem is not solved the program gives me the wrong output. i need help on the calculation formular.

GUYS I NEED YOUR HELP
Reputation Points: 10
Solved Threads: 0
Newbie Poster
loeto is offline Offline
14 posts
since Apr 2008
Apr 27th, 2008
0

Re: Please help i am new in c++ programming

You might want to repost your program (using code tags) to see if it really was fixed.

Also, you may want to change something like
C++ Syntax (Toggle Plain Text)
  1. first = number/1000;
to
C++ Syntax (Toggle Plain Text)
  1. first = floor(number/1000);

floor() is declared in math.h (so add #include<cmath> or #include<math.h> to the top of your source). i think it would be better than simply relying on the integer round down, but it won't really change your program's results.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Apr 27th, 2008
0

Re: Please help i am new in c++ programming

Secondly . LOETO
I think you should remove

C++ Syntax (Toggle Plain Text)
  1. cin >> number,first,second,third,fourth;
Or
C++ Syntax (Toggle Plain Text)
  1. cin >> number>>first>>second>>third>>fourth;

And just use the

C++ Syntax (Toggle Plain Text)
  1. cin>>number;

As YOU only require to take in one number each time and get the first,second,third and fourth out from the main number.
Last edited by Sky Diploma; Apr 27th, 2008 at 4:08 pm. Reason: Sorry For Posting Wrong Info.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Apr 27th, 2008
0

Re: Please help i am new in c++ programming

As for the floor() thing, I think you are getting floor() confused with a rounding function. floor() always rounds down, so floor(3.812) would be 3, not 4. conversely, ceil() always rounds up. so ceil(3.224) would be 4, not 3.

if you used abs() to truncate the non-integral part of the number, you would just be doing the same thing that he's currently doing, only doing indirectly through the abs() function, and not on your own.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Apr 27th, 2008
0

Re: Please help i am new in c++ programming

Well i am sorry .. MAde a bad comment on that one. I respond with DEEP apologies.

And edit my post as well
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Apr 27th, 2008
0

Re: Please help i am new in c++ programming

Well i am sorry .. MAde a bad comment on that one. I respond with DEEP apologies.

And edit my post as well
No need to apologize - just pointing it out. Everybody makes mistakes.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ 2D vector help please.
Next Thread in C++ Forum Timeline: New to C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC