Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 C++ Standard Input Help
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Panphobia




PostPosted: Thu Feb 28, 2013 5:19 pm   Post subject: C++ Standard Input Help

I am doing this question on an online judging website http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=996 , and I was doing it in java, but no matter what I did it was always a runtime error, when I followed all guidelines, now I switched to c++, my understand is that you need to subtract the two values and take the absolute value, is this wrong, is my standard input wrong, what is wrong?
c++:
#include <iostream>
using namespace std;
int main() {
    int a,b;
    while(cin >>a>>b){
        cin>>a>>b;
        cout << abs(b-a) << endl;
    }
    return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Feb 28, 2013 5:42 pm   Post subject: RE:C++ Standard Input Help

There's at least three things wrong here:

1) You didn't read the problem carefully enough. You don't need abs(), you just need subtraction.

2) You are reading from input twice for every iteration of your loop.

3) The correct solution to "my code doesn't work" is almost never "try a different language".
Panphobia




PostPosted: Thu Feb 28, 2013 5:56 pm   Post subject: RE:C++ Standard Input Help

Ahhhhhh thanks, I figured just now I was reading twice, but what I read was that you need the difference, and it says for the input that These two numbers in each line denotes the number of soldiers in Hashmat's army and his opponent's army or vice versa, so how are you supposed to know which is which?
DemonWasp




PostPosted: Thu Feb 28, 2013 6:04 pm   Post subject: RE:C++ Standard Input Help

Ah, okay, "or vice versa". That means you do need the abs(). Apparently I didn't read the problem very carefully.
Panphobia




PostPosted: Thu Feb 28, 2013 6:05 pm   Post subject: RE:C++ Standard Input Help

Well, then I don't know what is wrong with my code now, because I am reading it once, doing abs();, and it says wrong answer
c++:
#include <cstdlib>
#include <iostream>
using namespace std;

/*
 *
 */

int main() {
    int a,b;
    while(cin >>a>>b){
        cout << abs(b-a) << endl;
    }
    return 0;
}
Zren




PostPosted: Thu Feb 28, 2013 6:14 pm   Post subject: RE:C++ Standard Input Help

What range does the int data type cover?
Panphobia




PostPosted: Thu Feb 28, 2013 6:24 pm   Post subject: RE:C++ Standard Input Help

-2147m to 2147m, ahhh alright we need a long
Zren




PostPosted: Thu Feb 28, 2013 7:01 pm   Post subject: RE:C++ Standard Input Help

Considering you can't have a negative amount here, I'd suggest you use an unsigned data type.

Actually, nevermind. I'm fairly sure that subtraction between two unsigned values a - b where b > a would overflow.

Carry on.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: