intro to string classes/function hw

It's finals week and I admit I didnt study enough for this chapter. We are learning about string classes and functions. My first assignment in the unit is to write a program that will take a city and a Canadian province's abbreviation (ex. "Moose Jaw">>"AB") and output the same city name and the province's full name name (ex."Moose Jaw, Alberta.) It has to store both values in strings, and contain a function to compare the abbreviations and find the full name, and the final display must use concatenation. I put together this monster and it just feels hideously clunky (although it works) and I feel I must be missing some uses of string functions that would streamline this. anyone who takes the time to look at this thank you in advance!

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  #include <iostream>
#include <string>
using namespace std;

string province(string); 

int main(){
	string city, abbrev, prov;
	
	cout<<"Enter city: ";
	getline(cin,city);
	cout<<"Enter province abbreviation: ";
	cin>>abbrev;
	
	prov = province(abbrev);
	
	cout<<city+prov;
	
	return (0);
}

string province (string abbrev){
	
	struct provNames{
		
		string abr;
		string full;
		
	}AB, NB, NT, PQ, BC, NF, ON, SK, MB, NS, PE, YK;
	
	AB.abr="AB";
	AB.full=", Alberta";
	NB.abr="NB";
	NB.full=", New Brunswick";
	NT.abr="NT";
	NT.full=", Northwest Territories";
	PQ.abr="PQ";
	PQ.full=", Quebec";
	BC.abr="BC";
	BC.full=", British Columbia";
	NF.abr="NF";
	NF.full=", Newfoundland";
	ON.abr="ON";
	ON.full="Ontario";
	SK.abr="SK";
	SK.full="Saskatchewan";
	MB.abr="MB";
	MB.full="Manitoba";
	NS.abr="NS";
	NS.full="Nova Scotia";
	PE.abr="PE";
	PE.full="Prince Edward Island";
	YK.abr="YK";
	YK.full=", Yukon";
	
	if (abbrev == AB.abr){
		return (AB.full);
	}
	else if (abbrev == NB.abr){
		return (NB.full);
	}
	else if (abbrev == NT.abr){
		return (NT.full);
	}
	else if (abbrev == PQ.abr){
		return (PQ.full);
	}
	else if (abbrev == BC.abr){
		return (BC.full);
	}
	else if (abbrev == NF.abr){
		return (NF.full);
	}
	else if (abbrev == ON.abr){
		return (ON.full);
	}
	else if (abbrev == SK.abr){
		return (SK.full);
	}
	else if (abbrev == MB.abr){
		return (MB.full);
	}
	else if (abbrev == NS.abr){
		return (NS.full);
	}
	else if (abbrev == PE.abr){
		return (PE.full);
	}
	else if (abbrev == YK.abr){
		return (YK.full);
	}
}
I put together this monster and it just feels hideously clunky (although it works) and I feel I must be missing some uses of string functions that would streamline this.

Yea, agreed.

I would say that your major problem is not to do with strings but with how you're using that structure.

Have you studied std::map()? It would make things much easier.

Even a std::vector of that structure would make things easier.

Topic archived. No new replies allowed.