Displaying an Error Message

I'm trying to prompt an error message stating that when the user puts nothing into "lotName," it will spit out an error message. The error messages are stored as a function, but I'm not quite sure how to pull it out. Would I need an if statement here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	char lotName;
	cout << "Enter lot name: ";
	cin >> lotName;

	return 0;

}

void printErrorMessage(int num)
{
if (num == 3) 
{
cout << endl
 << "  ERROR: Blank bidder ID, no bid allowed" << endl 
}
}
1
2
3
4
if (lotName == "")
{
  printErrorMessage(3);
}
I'm not a C++ expert, but I'll try to help.

1st, lotName will take only 1 character, not the full name.
I think you should do this: char* lotName; /*or*/ char lotName[]; instead char lotName

I think this is what you should do:"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
void printErrorMessage(int num){
    if (num == 3)
    {cout << endl<< "  ERROR: Blank bidder ID, no bid allowed" << endl; }
}

int main()
{

	char* lotName;
    cout << "Enter lot name:";
    cin >> lotName;
    if (lotName=="") {printErrorMessage(3);}
	
	return 0;

}


EDIT: Ninja'd
Last edited on
Thanks for the replies!

I'm still getting an error message stating that "lotName" is not being initialized. What does this mean?
I see that lotName is a single char in your code (and a char pointer to random memory in ihato's code, which is also bad, although he does explain at the top of his post that it's not good). This is a fantastically bad idea if you want to store a C-string in it. It won't work. You will stomp all over memory that isn't yours.

I suggest you stop trying to use C-strings and use a proper C++ string.

Last edited on
Alright, I can see that. If for example a person were to input a string like "1980 Chevrolet," how would I exactly declare that?
1
2
string userEntry;
cin >> userEntry;
thank you! I did not know it was something as easy as that.

Much appreciated
Topic archived. No new replies allowed.