954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Help....plez.

Greetings C++ Lovers:

I'm a student very new to this C++. Yes I took Linux & UNIX. That was a year ago and I've never applied it to ANYTHING. If it's not too much trouble, I'm at a loss. A Big LOSS. You see, the reason I say this is bcuz. I took the initiative to go to my instructor to try to understand these C++ fundamentals.......and guess what.......It was still like reading this..."hjgddwufweuifdhweuf"! YEA!!! He tried breaking it down for me, now that I'm here (AT HOME) trying to figure this out I'm lost and DON'T KNOW WHAT I'm DOING :mad:

I've been given an assignment to write a program that asks the user to enter one of the following state abbreviations NC, SC, GA, FL, or AL. This program should display the name of the state that corresponds with the abbrev. Entered. Can you please help me? PLEASE? This IS frustrating. AM I to start out like this?....

char STATE[5];            /*   State abbreviation*/
{"North Carolina", "NC"},
{"South Carolina", "SC"}, 
{"Georgia", "GA"}, 
{"Florida", "Fl"},
{"Alabama", "Al"},
};
cout<<endl <<endl;


Please forgive for my ignorance to this course.

Your assistance means a whole lot!:sad:

Sincerely,

AnG’

AnG'
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Hmm maybe you need to look up on associative arrays HERE
But with your sparse knowledge of C++....

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Here is a starter. I would create a structure for the two strings, then array of that structure.

#include <iostream>
using namespace std;

struct states
{
	char *stname;
	char *stabbr;
};

states st[] = { /* State abbreviation*/
"North Carolina", "NC",
"South Carolina", "SC", 
"Georgia", "GA", 
"Florida", "Fl",
"Alabama", "Al",
};


int main(int argc, char* argv[])
{
	int array_size = sizeof(st) / sizeof(st[0]);
	for(int i = 0; i < array_size; ++i)
	{
		cout << st[i].stname << "\t" << st[i].stabbr << endl;
	}
	return 0;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

And along with the above code provided by Mr. Dragon, you might want to use fgets( char* my_array, SIZEOFARRAY, stdin ) to accept input from the user and a function which searches the entire structure array for the given string and then returns its state equivalent.

But personally i would advice you to go with the associative array or the STL map construct, unless you have been instructed not to do so, since it makes the code less cluttered, more logical and extensible. One thing you would never want to do in programming is to reinvent the wheel.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

I will try that. Thank you so much. And you are correct. When it comes to C++, I am "dense" in this region. I truely thank you for your time though. I was sincerely considering withdrawing from this class, but due to some compassionate collaborators, I'm liberated!


Yours Truely,

AnG'

AnG'
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You