how i finnish this ?????

need help on this program what i need to finnish it ?


#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>

using namespace std;
int escolherilha(string);

int main(int argc, char** argv) {

string ilha[9];
ilha[8] = "Corvo";
ilha[7] = "Flores";
ilha[6] = "Faial";
ilha[5] = "Pico";
ilha[4] = "Sao Jorge";
ilha[3] = "Graciosa";
ilha[2] = "Terceira";
ilha[1] = "Sao Miguel";
ilha[0] = "Santa Maria";
int dis[9][9] = {
{0, 102, 246, 339, 330, 330, 356, 589, 602},
{102, 0, 170, 246, 246, 276, 511, 519},
{261, 170, 0, 81, 93, 96, 124, 352, 564},
{339, 246, 81, 0, 61, 78, 85, 280, 282},
{330, 246, 93, 61, 0, 19, 39, 267, 272},
{330, 246, 96, 78, 19, 0, 9, 265, 274},
{356, 276, 124, 85, 39, 9, 0, 246, 257},
{589, 511, 352, 280, 267, 265, 246, 0, 24},
{602, 519, 564, 282, 272, 274, 257, 24, 0}
};
int total = 0, i = 0, ilhas = 0;
int linha = escolherilha("Insira a ilha de origem: ");
do {
int coluna = escolherilha("Insira a proxima ilha: ");
total += dis[ilha][coluna];
linha = coluna;
} while (i++ < ilhas);
cout << " a distancia e de " << total << endl;
return 0;
}

int escolherilha(string msg) {
int primeira = 0, ilha = 0;
int ref;
if (primeira) {
cin.ignore();
primeira = false;
}
do {
cout << msg << endl;
getline(cin, ilha);
ref = 9;
if (ilha == "Santa Maria")
ref = 0;
if (ilha == "Sao Miguel")
ref = 1;
if (ilha == "Terceira")
ref = 2;
if (ilha == "Graciosa")
ref = 3;
if (ilha == "Sao Jorge")
ref = 4;
if (ilha == "Pico")
ref = 5;
if (ilha == "Faial")
ref = 6;
if (ilha == "Flores")
ref = 7;
if (ilha == "Corvo")
ref = 8;
} while (ref < 1 || ref > 8);
return ref;
}
Could you ask a specific question? What is the purpose of you're program?
forget the other part now i need to find the small way to go to the other places without totching the first one and in the end i need to go to the start point like ( Santa Maria, sao miguel,terceira, graciosa, sao jorge, pico,faial,flores,corvo. Santa Maria) and discover the shorter way to go to that places.. can you help me ? here the new code i did....

/*
* File: main.cpp
* Author: neoazores
*
* Created on 16 de Dezembro de 2010, 20:35
*/

#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
using namespace std;
int escolherilha(string);

/*
*
*/
int ref, ilha;
int main(int argc, char** argv) {
int dis[9][9] = {
{0, 102, 246, 339, 330, 330, 356, 589, 602},
{102, 0, 170, 246, 246, 276, 511, 519},
{261, 170, 0, 81, 93, 96, 124, 352, 564},
{339, 246, 81, 0, 61, 78, 85, 280, 282},
{330, 246, 93, 61, 0, 19, 39, 267, 272},
{330, 246, 96, 78, 19, 0, 9, 265, 274},
{356, 276, 124, 85, 39, 9, 0, 246, 257},
{589, 511, 352, 280, 267, 265, 246, 0, 24},
{602, 519, 564, 282, 272, 274, 257, 24, 0}
};
int total;
int island = escolherilha("Starting island.");
cout << ref << endl;
for (int i = 0; i < 8; i++) {
total += dis[island][i];
}
cout << total << endl;


return 0;
}

int escolherilha(string msg) {
//int primeira = 0, ilha = 0;
//string ilha;

/*if (primeira) {
cin.ignore();
primeira = false;
}*/
// do {
cout << msg << endl;
cin >> ilha;
//ref = 9;
if (ilha == 0)
ref = 0;
if (ilha == 1)
ref = 1;
if (ilha == 2)
ref = 2;
if (ilha == 3)
ref = 3;
if (ilha == 4)
ref = 4;
if (ilha == 5)
ref = 5;
if (ilha == 6)
ref = 6;
if (ilha == 7)
ref = 7;
if (ilha == 8)
ref = 8;
//} while (ref < 1 || ref > 8);
return ref;
}

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Tiedosto: tärkein.cpp
* Kirjoittaa: neoazores
*
* Luotua 16 dettä Dezembro teellä 2010, 20:35
*/

#sisältää <cstdlib>
#sisältää <iostream>
#sisältää <string>
#sisältää <stdio.h>
#sisältää <time.h>
namespacella std;
int escolherilha(string);

/*
*
*/
int ref, ilha;
int sähköverkko (int argc, hiiltyy** argvin) {
int dis[9][9] = {
{0, 102, 246, 339,330, 330, 356, 589, 602},
{102, 0, 170, 246, 246, 276, 511, 519},
{261, 170, 0, 81, 93, 96, 124, 352, 564},
{339, 246, 81, 0, 61, 78, 85, 280, 282},
{330, 246, 93, 61, 0, 19, 39, 267, 272},
{330, 246, 96, 78, 19, 0, 9,265, 274},
{356, 276, 124, 85, 39, 9, 0, 246, 257},
{589, 511, 352, 280, 267, 265, 246, 0, 24},
{602, 519,564, 282, 272, 274, 257, 24, 0}
};
int summa;
int saari = escolherilha (” saaren Käynnistäminen”.);
cout << ref << endl;
silmukka (int i= 0; i<8; i++) {
summa += dis[island][i];
}
cout <<summa <<endl;


palauta 0;}

int escolherilha (naru msg) {
//int primeira = 0, ilhaa = 0;
//naru ilha;

/*jos (primeira) {
cin.ignore();
primeira = väärä;
}*/
// tekee {
cout << msg << endl;
cin >> ilha;
//ref = 9;
jos (ilha == 0) ref = 0;
jos (ilha == 1) ref = 1;
jos (ilha == 2) ref = 2;
jos (ilha == 3) ref = 3;
jos (ilha == 4) ref = 4;
jos (ilha == 5) ref = 5;
jos (ilha == 6) ref = 6;
jos (ilha == 7) ref = 7;
jos (ilha == 8) ref = 8;
//} kun (ref < 1 || ref > 8);
paluu ref;
}


I "finnish"'d it.

On-topic
Place your code in code tags and indent it properly, which makes your code a lot more readable. Although I've only heard a vague description, and still don't see how your approach would do anything.. I do see some things that are obviously wrong:
In escolherilha, you put ilha behind a comment line, so cin >> ilha; is an illegal action. Secondly, it doesn't make sense to use the comparisons like that; I believe you want this instead:
1
2
3
if (ilha == '0') ref = 0;
if (ilha == '1') ref = 1;
// etc. 


Edit:
Missed the ilha at the top. Yet, another point arises, why check every single value and set it to a new variable when it's already in a variable you aren't going to change?
1
2
3
4
5
6
7
int escolherilha(string msg) {
cout << msg << endl;
do {
cin >> ilha;
} while (ilha < 0 || ref > 8); // Should be <0 and not <1.
return ref;
}

Last edited on
Topic archived. No new replies allowed.