There were and abundance of mistakes but that's how you learn :)
1) I've seen many people do
using namespace std;
This is fine until you are working on a huge project which may have multiple libraries that have a function with the same name. How can the compiler tell them apart?
(More on this here: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Anyway,
using namespace std
is considered bad programming practice.
FIX:
1 2
|
using std::cout; using std::cin; using std::endl;
using std::string;
|
2) NEVER declare global variables unless you use them in multiple functions. Again, when working on a big project, you could accidentally use a variable name in a function that just so happens to have the same name as the global variable. You meant to use the global variable in your function, but the compiler doesn't know that. Instead, it chooses the local variable (in side your function) and your program doesn't do what you want.
FIX:
Move the variables into the function where it's used.
3) A
char
consists of only a single letter. If you want a word(more than a letter) aka a string, use the string variable instead. You've included the string library, but didn't use a string (???).
FIX:
1 2
|
string broadcastType
string strBroadcast;
|
4) In your
switch(broadcastType)
, since a
char
is only a single character you can't do
case 'word':
as 'word' is more than a character(it's 4 characters, aka a string). Also since the broadcastType is a string now, use
if/else if/else
statements.
http://stackoverflow.com/questions/650162/why-switch-statement-cannot-be-applied-on-strings
FIX:
1 2 3 4 5 6
|
if(broadcastType == "word")
letterBroadcast();
else if(broadcastType == "num")
numberBroadcast();
else
cout << "Please re run this program and answer word or num" << endl;
|
Note the use of double quotation marks (""). This is because broadcastType is a
string
. If it were a
char
using single quotation marks ('').
5) Your
char letterBroadcast()
and
int numberBroadcast()
don't actually return anything. Because of this, you have the return type as
void
.
(Example: https://ideone.com/kSbRTt)
FIX:
1 2
|
void letterBroadcast()
void numberBroadcast()
|
These functions are also incomplete (???). You had to store the what the user typed and then print it out again, right?
FIX:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void letterBroadcast()
{
string strBroadcast;
cout << " what is your 1 word broadcast?" << endl;
cin >> strBroadcast;
cout << strBroadcast << endl;
}
void numberBroadcast()
{
int intBroadcast;
cout << "What is your 1 number broadcast (no spaces)?" << endl;
cin >> intBroadcast;
cout << intBroadcast << endl;
};
|
I think that's all there is. I probably missed out something because I'm really tired. We were all complete beginners at one point, but with time you'll get there. I'm still learning C++ myself, so I have a lot to to learn. Please correct any of my mistakes.
Thanks :D