Please help

I was trying to make a project with no help where ai can choose a letter or number broadcast and if I choose number I can type in e.g: 17373837364 and that is supposed to print out and the same with letters except if I type in: abcdefghijklmnopqrstuvwxyz for example please help, I have been learning C++ for only 5 weeks so me writing this all by myself I think is pretty impressive but it doesn't work so,
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
#include <iostream>
#include <string>f
using namespace std;
 // Declaring Variables
//Reason it’s char is because I want you to say word or number
char broadcastType;
// This is a character broadcast where you can broadcast single words
char charBroadcast;
/* This is an integer broadcast where you can broadcast a single number with unlimited numbers */
int intBroadcast;
 
 char letterBroadcast()
 {
   cout << " what is your 1 word broadcast?" << endl;
   cin charBroadcast;
   };
   
   int numberBroadcast()
   {
    cout << "What is your 1 number broadcast (no spaces)?" << endl;
   };
 
int main() {
cout <<  "What type of broadcast would you like (word or num)" << endl;
cin >> broadcastType;
  
  switch(broadcastType)
  {
   case 'word':
   		letterBroadcast();
   		break;
   	case 'num':
   		numberBroadcast();
   		break;
   	default:
   		cout << "Please re run this program and answer word or num" << endl;
   	}

 
 
  }
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
Last edited on
The last problem I had fixed i accidentally copied the old code, but thatnk you soooooo much, If i put it onto a website ill give credit to you for helping me, thank you so much
thanks for the help... worked on my I pad... but when I run the fixed code... when I try to broadcast sentences it doesn't work
Topic archived. No new replies allowed.