36 Replies - 1918 Views - Last Post: 29 May 2012 - 10:33 AM
#16
Re: this input format..
Posted 04 May 2012 - 12:42 PM
#17
Re: this input format..
Posted 04 May 2012 - 01:09 PM
an int/a char/an int/a char/an int/a char
with no spaces in between.
Actually the only problem is in the way you set up your loops.
I'm not sure why you wrote this loop
while (fi.a>0)
{
sum1+=fi.a%10;
fi.a=fi.a/10;
}
You seem to be trying to sum the digits of the number. Why?
After these statements
cin>> fi.a ;
cin>>fi.opp;
cin>>fi.b ;
cin>>fi.equal_sign;
cin>>fi.c;
cin>>fi.dot;
you have all the information you need for the first formula: fi.a is the first operand, fi.opp is the operator, fi.b is the second operand and fi.c is the supplied answer. The problem is that you have an infinite loop.
Instead of a do-while loop, use a while loop, and use the first input statement to control the while loop.
Declare your struct before the beginning of the loop, and this will be the loop statement:
while(cin>>fi.a) {
and then inside the while loop, continue with the rest of the inputs, followed (still inside the loop) with all of the processing you need to check the answer.
Don't repeat cin>>fi.a inside the loop because that's already done in the while statement.
That control expression while(cin>>fi.a) will ensure that the loop continues as long as there are more inputs available in the file, and it will "fail" when you reach the final '.' because that can't be converted to an int, causing the looping to end.
#18
Re: this input format..
Posted 04 May 2012 - 01:18 PM
Quote
in some variable such as sum1...then the second number (6+7+8+9+0) and the third one with the same way.
so now I understand what you were doing in those 3 while loops.
But why do you want to do that? That wasn't requested in your assignment. The assignment says take 12345 and [add or subtract or multiply or divide] 67890 and test if the result equals 838102050. It doesn't ask you to add up the individual digits.
Your input sequence gives you the three integer values "ready to go", and next your code has to look at the value of fi.opp to decide which operation to perform on the first two numbers.
You're on the right track. Continue.
#19
Re: this input format..
Posted 04 May 2012 - 01:28 PM
r.stiltskin, on 04 May 2012 - 04:18 PM, said:
Quote
in some variable such as sum1...then the second number (6+7+8+9+0) and the third one with the same way.
so now I understand what you were doing in those 3 while loops.
But why do you want to do that? That wasn't requested in your assignment. The assignment says take 12345 and [add or subtract or multiply or divide] 67890 and test if the result equals 838102050. It doesn't ask you to add up the individual digits.
Your input sequence gives you the three integer values "ready to go", and next your code has to look at the value of fi.opp to decide which operation to perform on the first two numbers.
You're on the right track. Continue.
Come on guys ... Did any of you bother to read carefully the original post ?
And then ... perhaps google 'Hindu Check' ... to see what that was all about?
It invokes adding up the potentially long series of digits and getting a sum ... then taking sum modular 9 ...
So ... int sum may be ok but will need a string to hold a long series of digits to big for int or long long ...
This post has been edited by David W: 04 May 2012 - 01:40 PM
#20
Re: this input format..
Posted 04 May 2012 - 01:32 PM
#21
Re: this input format..
Posted 04 May 2012 - 01:38 PM
#22
Re: this input format..
Posted 04 May 2012 - 01:43 PM
#23
Re: this input format..
Posted 04 May 2012 - 01:59 PM
aresh, on 04 May 2012 - 04:32 PM, said:
If a type like long long were big enough to hold needed series of digits...
Using sstream ..,
istringstream iss( lineStr ); long long a, b, c; char op, dummy; iss >> a >> op >> b >> dummy >> c; // extract a, op, b, c hCheck( a, b, c, op );
Or ...
istringstream iss( lineStr ); Htest ht; char dummy; iss >> ht.a >> ht.op >> ht.b >> dummy >> ht.c; // extract a, op, b, c hCheck( ht );
Something like the above may be idea behind using a struct ?
#24
Re: this input format..
Posted 04 May 2012 - 02:47 PM
aresh, on 04 May 2012 - 01:32 PM, said:
Thank you aresh. I don't prefer using string because I'm just a beginner.
Please try my program it works well
#25
Re: this input format..
Posted 04 May 2012 - 02:51 PM
aresh, on 04 May 2012 - 04:32 PM, said:
If i is an int, cin >> i; will extract as many digits as possible that can be interpreted as an int, and stop when the next char is not a digit.
Next, if c is a char cin >> c; will extract exactly 1 char and stop.
Next, if j is an int, cin >> j; will extract as many digits as possible that can be interpreted as an int, and stop when the next char is not a digit.
Next, if d is a char cin >> d; will extract exactly 1 char and stop.
And so on.
And if i is an int, and the next thing in the input stream is not a digit, cin>>i; will fail (and set the fail flag) and "break" the loop.
Try it using some inputs like
1234+4321=5555.
73*22=1606.
6543-2222=4321.
.
#include <iostream>
using namespace std;
struct formula
{
int a;
char opp;
int b;
char equal_sign;
int c;
char dot;
};
int main()
{
formula fi;
while( cin >> fi.a ) {
cin>>fi.opp;
cin>>fi.b ;
cin>>fi.equal_sign;
cin>>fi.c;
cin>>fi.dot;
cout<<fi.a ;
cout<<fi.opp;
cout<<fi.b ;
cout<<fi.equal_sign;
cout<<fi.c;
cout<<fi.dot;
cout<<endl;
}
cout << "goodbye" << endl;
return 0;
}
Quote
Never heard of it. Too many things to do...too little time.
#26
Re: this input format..
Posted 04 May 2012 - 04:16 PM
r.stiltskin, on 04 May 2012 - 05:51 PM, said:
Quote
Never heard of it. Too many things to do...too little time.
But don't you put in mucho time here at DIC
I never heard of 'Hindu Check' before this either ... but since we at DIC so often recommend a Google Search ... thought I might take that advice myself ...
Best regards to all who so freely give at DIC
David
PS to the original poster:
1. Of the joys of learning to program and getting free help ... there's no place like DIC!
2. One of the first things you should note about OOP, if you want to learn it, is that you want to avoid storing redundant data in your object.
So, in this case ... all you need is the op and 3 number representations
3. If you wish to be a real C++beginner, then you should know that
C++ strings are designed especially for you ... and in light of your
example file having a product result of a five digit number times
another 5 digit number, then an int ( if it's 4 bytes ) is going to be
sometimes too small to hold that value. And extracting each 'digit'
in a string representation, is a whole lot easier to code also.
int sum( const string& s )
{
int len = s.size(), sum = 0;
for( int i = 0; i < len; ++i ) sum += s[i] - '0';
return sum;
}
This post has been edited by David W: 04 May 2012 - 05:24 PM
#27
Re: this input format..
Posted 04 May 2012 - 07:19 PM
#28
Re: this input format..
Posted 04 May 2012 - 07:22 PM
#29
Re: this input format..
Posted 04 May 2012 - 07:25 PM
r.stiltskin, on 04 May 2012 - 10:19 PM, said:
You are correct ... It is just a check. It will not confirm if valid answer ... only flag if invalid.
It's usefulness may be to 'lightly check' for invalidity of large number answers ?
#30
Re: this input format..
Posted 04 May 2012 - 07:28 PM
|
|

New Topic/Question
Reply




MultiQuote



|