954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please help i am new in c++ programming

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

#include <iostream>
using namespace std;

int main( )
//Initialize integers
{
int number,first,second,third,fourth,answer;

//For loop
for(int x=1;x <= 6;x++)

//Input of number from user   
    cout << "Enter in number: ";
    cin >> number,first,second,third,fourth;

//Get the first digit in the thousands place   
first = number / 1000;
number = number % 1000;

//Get the second digit in the hundreds place   
 second = number / 100;
 number = number % 100;
 
//Get the third digit in the tens place   
  third = number / 10;
  number = number % 10;
  
//Remaining is the ones place
  fourth = number;

//Calculate and give answer
  answer = first *3+second *third -fourth ;
  cout << "Your aswer is" << answer;
  cin >> answer;
  return 0;
  
}
loeto
Newbie Poster
14 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Its not

cin >> number,first,second,third,fourth;


but

cin >> number>>first>>second>>third>>fourth;


and kindly use code tags in future

hammerhead
Posting Whiz in Training
257 posts since May 2006
Reputation Points: 46
Solved Threads: 24
 

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

cin>>number;


your for loop doesnt look controlled, why are you prompting the user for "answer"??

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 

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

loeto
Newbie Poster
14 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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

loeto
Newbie Poster
14 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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

first = number/1000;

to

first = floor(number/1000);


floor() is declared in math.h (so add #include or #include 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.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

Secondly . LOETO
I think you should remove

cin >> number,first,second,third,fourth;

Or

cin >> number>>first>>second>>third>>fourth;


And just use the

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.

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

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.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

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

And edit my post as well

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

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.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

There is no need to use floor() as int truncates any decimal place as it is.

for any 4 digit number this should do the trick to extract all 4 digits

i=0;
while(number>0)
{
a[i]=number%10;
number=number/10;
i++;
}

i=0 will correspond to the last digit(4th in this case), i=1 to the 3rd and so on.

so your answer will be

ans=a[3]*3+a[2]*a[1]-a[0];
hammerhead
Posting Whiz in Training
257 posts since May 2006
Reputation Points: 46
Solved Threads: 24
 
There is no need to use floor() as int truncates any decimal place as it is.

Is that method of truncation considered "safe"? I thought it wasn't a good idea to let C++ take care of the truncation for you when you pass a non-integral number to an int.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

I am not sure, it has worked for me and I have never bothered to search. I will leave it for more experienced programmers to answer your question.

hammerhead
Posting Whiz in Training
257 posts since May 2006
Reputation Points: 46
Solved Threads: 24
 

Thanks again guys
but the way i understand this question i have to put all six integers at once this way :325510671393289211112020; & then i have to separate them and calculate as indicated 3255*3 +1067*1393 -2892 and i think the answer should be 1493204

loeto
Newbie Poster
14 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Er... at first you said the given integers will all consist of four digits. But you just mentioned six digits. And then you gave 325510671393289211112020, which is a 24 digit number, and then broke it up into six sections of four digits, treating each one like a "digit". Then you performed the digit operations that you mentioned (first*3 + second*third - fourth) on the first four of those "digits", and just ignored the last two. Whatexactly is the program supposed to do? Give an example of input, output, and an explanation, because what you just said contradicts what you said originally.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

Sorry , like i said still new in programming


but the way i understand this question i have to put all six integers at once this way :325510671393289211112020; & then i have to separate them and calculate as indicated 3*3 +2*5 -5

little bit confused

loeto
Newbie Poster
14 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Do you have the exact prompt with you? Could you post it, because I doubt that all six of the inputs would be put in at once without any breaks. I think they might mean that the digits within each of the six numbers need to be taken it at once, but not all the numbers together.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

:icon_mrgreen: I made a much more efficient version using arrays (I don't know if you have used arrays but try to understand, it's quiet simple)

#include
using namespace std;
int main()
{
int number[4];


cout << "Enter number: ";
cin >> number[0];
cin >> number[1];
cin >> number[2];
cin >> number[3];
cout << "\n\nThe answer is: " << number[0]*3 + number[1]*number[2] - number[3] << "\n\n";
system ("pause");
return 0;
}

dark3lf
Newbie Poster
7 posts since Apr 2008
Reputation Points: 14
Solved Threads: 1
 

I believe every four digit number had to be taken in at once, not each digit separately (otherwise your approach works). But I'm not sure exactly, so I'm waiting for him to try and clarify exactly what the program needs to do.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

I think you are right about that the numbers need to be taken in all at once, but there is no way to do that using the cin function provided by #include . This requires the use of some other reference source or library like allegro.

dark3lf
Newbie Poster
7 posts since Apr 2008
Reputation Points: 14
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You