Please help i am new in c++ programming

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 14
Reputation: loeto is an unknown quantity at this point 
Solved Threads: 0
loeto loeto is offline Offline
Newbie Poster

Please help i am new in c++ programming

 
0
  #1
Apr 27th, 2008
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
  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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

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

 
0
  #2
Apr 27th, 2008
Its not
  1. cin >> number,first,second,third,fourth;

but
  1. cin >> number>>first>>second>>third>>fourth;

and kindly use code tags in future
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

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

 
0
  #3
Apr 27th, 2008
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 14
Reputation: loeto is an unknown quantity at this point 
Solved Threads: 0
loeto loeto is offline Offline
Newbie Poster

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

 
0
  #4
Apr 27th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 14
Reputation: loeto is an unknown quantity at this point 
Solved Threads: 0
loeto loeto is offline Offline
Newbie Poster

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

 
0
  #5
Apr 27th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

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

 
0
  #6
Apr 27th, 2008
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
  1. first = number/1000;
to
  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.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 100
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

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

 
0
  #7
Apr 27th, 2008
Secondly . LOETO
I think you should remove

  1. cin >> number,first,second,third,fourth;
Or
  1. cin >> number>>first>>second>>third>>fourth;

And just use the

  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.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

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

 
0
  #8
Apr 27th, 2008
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.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 100
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

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

 
0
  #9
Apr 27th, 2008
Well i am sorry .. MAde a bad comment on that one. I respond with DEEP apologies.

And edit my post as well
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

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

 
0
  #10
Apr 27th, 2008
Originally Posted by Sky Diploma View Post
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.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC