Write your question here.
Hello, I'm trying to solve this problem http://coj.uci.cu/24h/problem.xhtml?pid=2264
I think i know how to solve it but cannot translate it to code, in my head how this works is you declare and input all variables asked, then you need to do a for loop until you reach the number of moves the user input, but I do not know how to input a char and a number at the same time as show there. I also think you need to subtract from the last command down to the first one to find the original start point, and that moving East and West will result in a negative move in their respective axis, and West and North will result in positive. Can you guys help me with some tips/hints to solve this, thanks. (This is the code I have right now but I know its far from the answer and has errors compiling.)
#include <iostream>
usingnamespace std;
int main()
{
int n, m, x, y, count;
char N, S, E, W;
cin >> n >> m;
cin >> x >> y >>count;
cin >> N, S, E, W;
for(int i = 0; i==count; i++)
{
if(cin >> N)
N = y + 1;
elseif (cin >> S)
S = y - 1;
elseif(cin >> E)
E = x - 1;
elseif(cin >>W)
W = x + 1;
}
cout << x << y << endl;
return 0;
}
cin >> N, S, E, W; This is statement "read from standard input to N, then evaluate valuer of S E W and throw away results"
if(cin >> N) THis is statement "Read from standard stream into N, convert stream state to bool and continue if it is true"
As you can see it is not exactly what you want. Remember, variable names are only for user convention (and to refer to specific entity). Meaning of program will not change if you rename them. Now replace N S E W with A B C D. See your logic falling apart? That means it was wrong approach.
What you want to do is read a single character representing direction
Lets do it:
1 2
char direction;
std::cin >> direction;
Now you want to do some actions depending on value of direction. This can be done using chained if-else, but switch is a better choice here:
1 2 3 4 5 6
switch (direction) {
case'N': /*Handle N case*/; break;
case'S': /*Handle S case*/; break;
case'E': /*Handle E case*/; break;
case'W': /*Handle W case*/; break;
}
Ok, i understood what you explained, but how would I got about looping that switch until a the number of loops is equal to count, a for statement? And is there a way to input the char in the switch with an integer such as 2, 3, etc. to know how many steps the user took?
And is there a way to input the char in the switch with an integer such as 2, 3, etc
Why do you need it in char? Put it in another variable and use it later.
1 2 3 4 5 6 7 8 9 10
char direction;
int distance;
std::cin >> direction >> distance;
switch (direction) {
case'N': move(NORTH, distnace); break;
case'S': /*Handle S case*/; break;
case'E': /*Handle E case*/; break;
case'W': /*Handle W case*/; break;
}
how would I got about looping that switch until a the number of loops is equal to count, a for statement?
A canonical form of for statement: for(int i = 0; i < steps; ++i)
In the beginning of each iteration read next direction and distance and use it in switch.
So I tried doing what you told me but still I couldnt get it to work, I added some outputs to try and help me see where I was in my program, but still couldn't get it to work like its supposed to.
Ok, got it, it is almost working, my x position is working as intended, but y does not change, it stays the same as I defined it at the start of the program.
You're right, I though I had fixed but didn't, I did now and it works like it is supposed to, except the judge says I have the wrong answer(says wrong answer test 3) though I have the expected input and output :(
Jesus, thats such a dumb mistake there's even an image in the problem, lol. Well anyhow thank you very much for your helped I learned a ton of things and I finally got it right