Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

c++ - variable array value depending on increasing row number

A short description of my program that I am working on: I input a number n which means number of rows. a b variables stands for dimensions of rectangle - its second input. Then, most important part. I have a person which is standing on this rectangle and has a starting position x y and starting direction s. x y are in range as you can see in the code. Directions are S,J,V,Z (N,S,E,W). So this input is in 3rd row and it consists of 3 parameters: x y s. As you can see in my code, if I input S, my y value increases by +1, J decreases y value by -1, etc..

At the end of my code is a for loop, which outputs all inputs at once, because I don not want it output right away, after each input.

My problem is, I input:

2 // number of tasks or rows
5 8 // a b dimensions
Uloha 1 (Task 1):
4 5 S // x y s (S means y+1)
Uloha 2:
2 3 Z // x y s (Z means x-1)

Output:

1 3 Z // here should be 4 6 S

1 3 Z

I press enter, and I get two rows of output but the values of x y s are only final values of the second row, so it doesnt remember each task, logically. The last input value of x y s changes every time I input new row. And also, there is a if condition which is not working too, when x y goes under 0 or above a or b, it should print SPADOL.

What do you think, any suggestions? Btw, Im just a beginner in C++. Thank you.

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <string>

using namespace std;

int n; // pocet uloh
int a; // rozmer obdlznika a
int b; // rozmer obdlznika b
int x;
int y;
string s; // smer
int i;

vector<string> inputs;


int main() {

    cin >> n;

    while(!((n >= 1)&&(n <=15000)))
    {
        cout << "max 15000" << flush;
        cin >> n;
    }


    cin >> a >> b;

    while(!((a >= 1)&&(a <=100) | (b >= 1)&&(b <= 100)))
    {
        cout << "max 100" << flush;
        cin >> a >> b;
    }


    for (i = 0; i < n; i++)
    {    
        cout << "Uloha " << i+1 << ":" << endl;

        cin >> x;
        cin >> y;
        cin >> s;

        while(!((x>=0)&&(x<=a))) {
            cout << "Try Again x: " << flush;
            cin >> x;
        }
        while(!((y>=0)&&(y<=b))) {
            cout << "Try Again y: " << flush;
            cin >> y;
        }


        if (s == "S"){
            y = (y+1);
        }else if (s == "J"){
            y = (y-1);
        }else if (s == "V"){
            x = (x+1);
        }else if (s == "Z"){
            x = (x-1);
        }
        if(!((x>=0)&&(x<=a) | (y>=0)&&(y<=b))){
            cout << x << ' ' << y << ' ' << s << ' ' << "SPADOL" << endl;
        }


        // inputs.push_back(x);
        // inputs.push_back(y);
        // inputs.push_back(z);
        //for(vector<string>::iterator it = inputs.begin(); it != inputs.end(); ++it)
        //{ 
        //cout << x << ' ' << y << ' ' << *it << endl;
        //}

    }    // koniec for

    for ( i = 0 ; i < n ; i++ )
    {
        cout << x << ' ' << y << ' ' << s << endl;

    } 

system("pause");
}

EDIT:>

    #include <iostream>
        #include <string>
        #include <stdlib.h>
        #include <string>

        using namespace std;
        int n; // pocet uloh
        int a; // rozmer obdlznika a
        int b; // rozmer obdlznika b
        int *x = (int*)malloc(n*sizeof(int));
        int *y = (int*)malloc(n*sizeof(int));
        string *s = (string*)malloc(n*sizeof(string)); // smer
        int i;
        int d;

        static const char alpha[] = {'D', 'L', 'P'};
        char genRandom()
        {
          return alpha[rand() % strlen(alpha)];
        }
        // end of generator


        int main() {

         cin >> n;

         while(!((n >= 1)&&(n <=15000)))
         {
         cout << "max 15000" << flush;
         cin >> n;
    }


    cin >> a >> b;

    while(!((a >= 1)&&(a <=100) & (b >= 1)&&(b <= 100)&&(a!=b)))
    {
    cout << "chyba max 100 alebo a!=b" << endl;
    cin >> a >> b;
    }


    for (i = 0; i < n; i++)
          {    
    cout << "Uloha " << i+1 << ":" << endl;

    cin >> x[i];
    cin >> y[i];
    cin >> s[i];

    while(!((x[i]>=0)&&(x[i]<=a))) {
    cout << "Try Again x: " << flush;
    cin >> x[i];}
    while(!((y[i]>=0)&&(y[i]<=b))) {
    cout << "Try Again y: " << flush;
    cin >> y[i];}


    if (s[i] == "S"){
    y[i] = (y[i]+1);
    }else if (s[i] == "J"){
    y[i] = (y[i]-1);
    }else if (s[i] == "V"){
    x[i] = (x[i]+1);
    }else if (s[i] == "Z"){
    x[i] = (x[i]-1);
    }
    cin >> d;
    while(!((d>=1)&& (d<=200))) {
    cout << "Try Again d: " << flush;
    cin >> d;}


   for (int counter=0; counter<d; counter++)
    {
   cout << genRandom();
    }
cout << endl;

}    // koniec for

for ( i = 0 ; i < n ; i++ )
{
if(!((x[i]>=0)&&(x[i]<=a) & (y[i]>=0)&&(y[i]<=b))){
cout << x[i] << ' ' << y[i] << ' ' << s[i] << ' ' << "SPADOL" << endl;
}else{
cout << x[i] << ' ' << y[i] << ' ' << s[i] << endl;
}

} 

free(x);free(y);free(s);

system("pause");
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Put brackets when you use the or condition (a&&b) | (c&&d).

if(!(((x>=0)&&(x<=a)) | ((y>=0)&&(y<=b)))){

This code is working for me. You can use the c++ vector to store the values and print them at the end.

#include <iostream>
#include <string>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;
int n; // pocet uloh
int a; // rozmer obdlznika a
int b; // rozmer obdlznika b
int i;
int d;

static const char alpha[] = {'D', 'L', 'P'};
char genRandom()
{
    return alpha[rand() % strlen(alpha)];
}
// end of generator

int main() {

    cin >> n;
    vector<int> x(n);
    vector<int> y(n);
    vector<string> s(n);

    while(!((n >= 1)&&(n <=15000)))
    {
        cout << "max 15000" << flush;
        cin >> n;
    }


    cin >> a >> b;

    while(!((a >= 1)&&(a <=100) & (b >= 1)&&(b <= 100)&&(a!=b)))
    {
        cout << "chyba max 100 alebo a!=b" << endl;
        cin >> a >> b;
    }


    for (i = 0; i < n; i++)
    {    
        cout << "Uloha " << i+1 << ":" << endl;
        cout << "x: ";
        cin >> x[i];
        cout << "y: ";
        cin >> y[i];
        cout << "s: ";
        cin >> s[i];

        while(!((x[i]>=0)&&(x[i]<=a))) {
            cout << "Try Again x: " << flush;
            cin >> x[i];}
            while(!((y[i]>=0)&&(y[i]<=b))) {
                cout << "Try Again y: " << flush;
                cin >> y[i];}


                if (s[i] == "S"){
                    y[i] = (y[i]+1);
                }else if (s[i] == "J"){
                    y[i] = (y[i]-1);
                }else if (s[i] == "V"){
                    x[i] = (x[i]+1);
                }else if (s[i] == "Z"){
                    x[i] = (x[i]-1);
                }
                cin >> d;
                while(!((d>=1)&& (d<=200))) {
                    cout << "Try Again d: " << flush;
                    cin >> d;}


                    for (int counter=0; counter<d; counter++)
                    {
                        cout << genRandom();
                    }
                    cout << endl;

    }    // koniec for

    for ( i = 0 ; i < n ; i++ )
    {
        if(!((x[i]>=0)&&(x[i]<=a) & (y[i]>=0)&&(y[i]<=b))){
            cout << x[i] << ' ' << y[i] << ' ' << s[i] << ' ' << "SPADOL" << endl;
        }else{
            cout << x[i] << ' ' << y[i] << ' ' << s[i] << endl;
        }

    } 


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...