Page 1 of 1

Hey, pls hlp cause im dumb. :)

Posted: September 26th, 2017, 10:01 pm
by Alacaster
All you need to know is that this program is meant to ask if the coordinates x, y have to be visited.
then it is supposed to print out all of the coordinates that need to be visited. input r to skip to print.

Mainly what happens is when it prints it only prints coordinates where the y is 0.
it does some other weird stuff, when you don't finish it prints x (0-max) and y (Huge number)

Have fun

Code: Select all

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

using namespace std;

int main()
{
    bool city[5][6] = {0};
    char buffer;
    int Cities = 0;
    unsigned int i = 0;
    unsigned int r = 0;

    char Comformation_Buffer;
    cout << "Welcome to the traveling salesman algorithm. It is to figure out the most efficient path between all \n of the diffrent places  that the salesman must visit \n" << endl;
    for(r = 0; r <= 5; r++){ if(buffer == 'r'){continue;}
        for(i = 0; i <= 6; i++)
        {
                cout << "do the coordinates " << r << ", " << i << " need to be visited by the salesman. (Y)es/(N)o \n";
                cin >> buffer;

                if(buffer == 'r'){break;} //escape route

                if(buffer == 'y' || buffer == 'Y'){city[r][i] = true; Cities++;}
                    else{city[r][i] = false;}

                if(city[r][i] == true){cout << "the coordinates " << r << ", " << i << " need to be visited by the salesman. \n";}
                    else{cout << "the coordinates " << r << ", " << i << " do not need to be visited by the salesman. \n";}

                cout << "is this correct? (Y)es/(N)o "<< endl;
                cin >> Comformation_Buffer;

                if(Comformation_Buffer == 'n' || Comformation_Buffer == 'N'){
                    if(i == 0){--r; i=6;}else{i--;}}

        }
                }

        int coor[Cities][1] = {0};
        int_fast16_t z = 0;
        cout << "The coordanates of each city are \n";

        for(r = 0; r <= 5; r++){
            for(i = 0; i <= 6; i++){
                    if(city[r][i]==true)
                        {coor[z][0]=r;
                        coor[z][1]=i;
                        z++;
                        cout << coor[z][0] << ", " << coor[z][1] << endl;}
                        }

        } system("pause");
    return 0;
}

Re: Hey, pls hlp cause im dumb. :)

Posted: September 26th, 2017, 11:47 pm
by Yumtard
This doesn't even build for me.

int coor[Cities][1] = { 0 };

cities needs to be a constant value

Re: Hey, pls hlp cause im dumb. :)

Posted: September 27th, 2017, 12:34 am
by Alacaster
#define CITIES Cities
int coor[CITIES][1] = {0};

Re: Hey, pls hlp cause im dumb. :)

Posted: September 27th, 2017, 12:59 am
by albinopapa
#define CITIES Cities
int coor[CITIES][1] = {0};

This doesn't mean anything other than replace CITIES with Cities and if Cities isn't a constant value like
#define Cities 10 or
constexpr int Cities = 10;
then the compiler will still complain that CITIES is not a constant value.

Another issue is if this declaration was to be instantiated, it would be an array of 1 element
int coor[ Cities ][ 1 ] = { 0 };

So the second line would be out of bounds
coor[ z ][ 0 ] = r;
coor[ z ][ 1 ] = i;

Re: Hey, pls hlp cause im dumb. :)

Posted: September 27th, 2017, 1:30 am
by albinopapa
Another issue would be the fact you set the city array to bool city[ 5 ][ 6 ] = { 0 };
While in your for loops you do a <= comparison which means you are indexing past the end of the arrays. An array of 5 elements goes from 0-4 {0,1,2,3,4} so it should be just
for( unsigned int r = 0; r < 5; r++ )
for( unsigned int i = 0; i < 6; i++ )

Re: Hey, pls hlp cause im dumb. :)

Posted: September 27th, 2017, 1:45 am
by albinopapa
if(buffer == 'r'){continue;}
This line seems odd to me. This is between the outer and inner for loops before you get any input from user. When you do get input from user, you immediately check for == 'r' and break out of the inner loop and when execution reaches this line you continue to the next outer loop which go back to this line and since the buffer hasn't been cleared, it will keep skipping the inner loop. Why not just break out of the outer loop as well instead of continuing if you are never going to process the inner loop again after someone presses 'r'?