this input format..

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 2149 Views - Last Post: 29 May 2012 - 10:33 AM Rate Topic: -----

#16 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: this input format..

Posted 04 May 2012 - 12:42 PM

What is the use of making a structure if you are going to use only one copy of it ?? I find it quiet useless. You can declare them normally and also access them normally. As far as the program you wrote is concerned, I find it quiet amusing. How did you except cin to do all the work for you ?? We have been telling you numerous ways to get the input in a string and parse it, and you write such code... *sigh* cin is not so intelligent that it can separate the numbers, operators, '=' all by itself. If only coding was so easy.... :D
Was This Post Helpful? 0
  • +
  • -

#17 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: this input format..

Posted 04 May 2012 - 01:09 PM

Your use of a struct in this case is actually very clever, and because of the unique layout of your input file, cin actually can do all the work for you, since your file layout is

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.
Was This Post Helpful? 1
  • +
  • -

#18 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: this input format..

Posted 04 May 2012 - 01:18 PM

Ah, I just noticed this in Post #1:

Quote

I want the program to add each unit of the first number like this (1+2+3+4+5) and place the result
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.
Was This Post Helpful? 0
  • +
  • -

#19 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: this input format..

Posted 04 May 2012 - 01:28 PM

View Postr.stiltskin, on 04 May 2012 - 04:18 PM, said:

Ah, I just noticed this in Post #1:

Quote

I want the program to add each unit of the first number like this (1+2+3+4+5) and place the result
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

Was This Post Helpful? 1
  • +
  • -

#20 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: this input format..

Posted 04 May 2012 - 01:32 PM

@r.stiltskin : What is clever about the use of structure in the code ?? That you have to address every variable by writing fi.variable ?? And, how in the world can cin actually recognize the difference b/w a char and a number, not even separated by whitespace ?? If you declare an int a, and write cin>>a; and try to input a character, it will happily accept it and crash/produce garbage result. So how in the world can it differentiate b/w these diff. types of inputs ??
Was This Post Helpful? 0
  • +
  • -

#21 Invincible-99  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 03-May 12

Re: this input format..

Posted 04 May 2012 - 01:38 PM

I want the sum of the digits to do the hindu check.
Posted Image
Was This Post Helpful? 0
  • +
  • -

#22 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: this input format..

Posted 04 May 2012 - 01:43 PM

View PostInvincible-99, on 04 May 2012 - 04:38 PM, said:

I want the sum of the digits to do the hindu check.
Posted Image


You see how easily one can miss some necessary info :)

This post has been edited by r.stiltskin: 04 May 2012 - 07:25 PM

Was This Post Helpful? 1
  • +
  • -

#23 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: this input format..

Posted 04 May 2012 - 01:59 PM

View Postaresh, on 04 May 2012 - 04:32 PM, said:

@r.stiltskin : What is clever about the use of structure in the code ?? That you have to address every variable by writing fi.variable ?? And, how in the world can cin actually recognize the difference b/w a char and a number, not even separated by whitespace ?? If you declare an int a, and write cin>>a; and try to input a character, it will happily accept it and crash/produce garbage result. So how in the world can it differentiate b/w these diff. types of inputs ??

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 ?
Was This Post Helpful? 1
  • +
  • -

#24 Invincible-99  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 03-May 12

Re: this input format..

Posted 04 May 2012 - 02:47 PM

View Postaresh, on 04 May 2012 - 01:32 PM, said:

@r.stiltskin : What is clever about the use of structure in the code ?? That you have to address every variable by writing fi.variable ?? And, how in the world can cin actually recognize the difference b/w a char and a number, not even separated by whitespace ?? If you declare an int a, and write cin>>a; and try to input a character, it will happily accept it and crash/produce garbage result. So how in the world can it differentiate b/w these diff. types of inputs ??

Thank you aresh. I don't prefer using string because I'm just a beginner.
Please try my program it works well :wink:
Was This Post Helpful? 0
  • +
  • -

#25 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: this input format..

Posted 04 May 2012 - 02:51 PM

View Postaresh, on 04 May 2012 - 04:32 PM, said:

@r.stiltskin : What is clever about the use of structure in the code ?? That you have to address every variable by writing fi.variable ?? And, how in the world can cin actually recognize the difference b/w a char and a number, not even separated by whitespace ?? If you declare an int a, and write cin>>a; and try to input a character, it will happily accept it and crash/produce garbage result. So how in the world can it differentiate b/w these diff. types of inputs ??

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

And then ... perhaps google 'Hindu Check' ... to see what that was all about?

Never heard of it. Too many things to do...too little time. :)
Was This Post Helpful? 1
  • +
  • -

#26 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: this input format..

Posted 04 May 2012 - 04:16 PM

View Postr.stiltskin, on 04 May 2012 - 05:51 PM, said:

...

Quote

And then ... perhaps google 'Hindu Check' ... to see what that was all about?

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

Was This Post Helpful? 1
  • +
  • -

#27 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: this input format..

Posted 04 May 2012 - 07:19 PM

This Hindu Check is interesting, but doesn't seem very useful. The test will spot an incorrect result. But a result that passes the test might also be incorrect.
Was This Post Helpful? 0
  • +
  • -

#28 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 576
  • View blog
  • Posts: 2,730
  • Joined: 24-July 11

Re: this input format..

Posted 04 May 2012 - 07:22 PM

That picture that the OP redirected me to a strange site… maybe it should be removed?
Was This Post Helpful? 0
  • +
  • -

#29 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: this input format..

Posted 04 May 2012 - 07:25 PM

View Postr.stiltskin, on 04 May 2012 - 10:19 PM, said:

This Hindu Check is interesting, but doesn't seem very useful. The test will spot an incorrect result. But a result that passes the test might also be incorrect.

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 ?
Was This Post Helpful? 1
  • +
  • -

#30 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: this input format..

Posted 04 May 2012 - 07:28 PM

View PostDimitriV, on 04 May 2012 - 10:22 PM, said:

That picture that the OP redirected me to a strange site… maybe it should be removed?

Fixed.
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3