Help with Program - For loop(?)

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.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace 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;
        else if (cin >> S)
            S = y - 1;
        else if(cin >> E)
            E = x - 1;
        else if(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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

int main()
{
    int n, m, x, y, count;
    
    char direction;
    
    int distance;
    
    cout << "size" << endl;
    cin >> n >> m;
    
    cout << "end point count" <<endl;
    cin >> x >> y >> count;
    
    cout << "enter direction" <<endl;
    cin >> direction;
    
    for(int i = 0; i == count; ++i)
        cout << "enter direction" << endl;
        cin >> direction >> distance;
            switch (direction) {
                case 'N': (y += distance ); break;
                case 'S': (y -= distance); break;
                case 'E': (x -= distance); break;
                case 'W': (x += distance); break;
    }
    cout <<"starting point is" << x << y << endl;
    
    return 0;
}
Why do you need lines 19-20? You should get rid of that.

Also your for loop is wrong. condition is referring to the loop continuation condition. You are saying "loop only if i == count " right now.
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

int main()
{
    int n, m, x, y, count;
    
    char direction;
    
    int distance;
    
    cout << "size" << endl;
    cin >> n >> m;
    
    cout << "end point count" <<endl;
    cin >> x >> y >> count;
    
    for(int i = 0; i < count; ++i)
        cin >> direction >> distance;
            switch (direction) {
                case 'N': (y += distance); break;
                case 'S': (y -= distance); break;
                case 'E': (x -= distance); break;
                case 'W': (x += distance); break;
    }
    cout << x << " " << y << endl;
    
    return 0;
}
You have braces problem.

Your code properly formatted:
1
2
3
4
5
6
7
8
9
for(int i = 0; i < count; ++i)
    cin >> direction >> distance;

switch (direction) {
    case 'N': (y += distance); break;
    case 'S': (y -= distance); break;
    case 'E': (x -= distance); break;
    case 'W': (x += distance); break;
}
Do you see problem now? Add braces around for statement body.
Yes, the problem is still the same, this is what my console looks like
1
2
3
4
5
6
7
8
9
10
size
3 3
end point count
3 3 5
E 1
S 1
W 1
S 1
E 2
1 3

This is what the problem says it should be
1
2
3
4
5
6
7
8
9
10
11
12
Sample input

3 3
3 3 5
E 1
S 1
W 1
S 1
E 2
Sample output

1 1

It seems my y varible is not working in the switch statement for some reason it just stays the same.
Did you correct the braces problem? I see that you do not in fact did it as only very last statement is executed.
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 :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

int main()
{
    int n, m, x, y, count;
    
    char direction;
    
    int distance;
    
    cin >> n >> m;
    
    cin >> x >> y >> count;
    
    for(int i = 0; i < count; ++i)
    {
        cin >> direction >> distance;
    
        switch (direction) {
            case 'N': (y += distance); break;
            case 'S': (y -= distance); break;
            case 'E': (x -= distance); break;
            case 'W': (x += distance); break;
        }
    }
    cout << x << " " << y << endl;
    
    return 0;
}
x is row number, i.e. vertical coordinate, y is horisontal one.
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
Topic archived. No new replies allowed.