Displaying Hazard level for calculated PSI

So guys, its a little hard to explain what I want so I'll try anyway. Basically this program is to calculate 3 hourly PSI value and show the hazard level of the PSI.

I am having trouble with the second part, the main problem is I want to use the PSI value that I got to check the Hazard level and that is the part of the code I just can't seem to get.

Obviously this is part of the code and not the full one
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <conio.h>
char chkPSI(int* PSI);
void SelectionAN(void)
{	
	extern int AN[];
	int selection2;
	int PSI = 0;
	char Hazard;
	{
		PSImenu();
		selection2 = _getch();
		switch(selection2)
		{
		case'1': case 1: 
			PSI = (AN[0] + AN[1] + AN[2]) / 3;
			Hazard = chkPSI(PSI);
			printf("\nPSI on 23rd June 2013, at 3am, for the North is %d\n", PSI);
			printf("%c", Hazard);
			break;


This is what I'm using to check the levels but i can't seem to display the message that I want.

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <conio.h>
char chkPSI(int* psi)
{
	char Hazard;
	if (0 <= psi || psi <= 50) Hazard = "Good";
	else if (51 <= psi || psi <= 100) Hazard = "Moderate";
	else if (101 <= psi || psi <= 200) Hazard = "Unhealthy";
	else if (201 <= psi || psi <= 300) Hazard = "Very Unhealthy";
	else Hazard = "Hazardous";
	return Hazard;
}


Any help at all will be appreciated and sorry if this seems a little too basic hehe.
In chkPSI lines 6-10, you're trying to assign a C-style string to a single character. You can't do that.

Use std::string instead:
1
2
3
4
5
6
7
8
9
10
#include <string>
std::string chkPSI (int* psi)
{   std::string Hazard;
    if (0 <= psi || psi <= 50) Hazard = "Good";
    else if (51 <= psi || psi <= 100) Hazard = "Moderate";
    else if (101 <= psi || psi <= 200) Hazard = "Unhealthy";
    else if (201 <= psi || psi <= 300) Hazard = "Very Unhealthy";
    else Hazard = "Hazardous";
    return Hazard;
}

Hi AbstractionAnon,

Forgive me for my ignorance but I am unfamiliar with std::string and when trying to use it, it has given me many errors. I was hoping you can elaborate more on how it can be used properly

thanks,
FL4me
I am unfamiliar with std::string

http://www.cplusplus.com/reference/string/string/

Are you using a C++ compiler? Afterall, this is a C++ forum.
Your headers and use of printf and C-style string seem to imply you're using C, not C++. std::string is a C++ feature.

If you can only use C, the best way to do this is to pass a pointer to a string array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string.h>    // C header
void chkPSI (int* psi, char * Hazard)
{   if (0 <= psi || psi <= 50) strcpy (Hazard, "Good");
    else if (51 <= psi || psi <= 100) strcpy (Hazard, "Moderate");
    else if (101 <= psi || psi <= 200) strcpy (Hazard, "Unhealthy");
    else if (201 <= psi || psi <= 300) strcpy (Hazard, "Very Unhealthy");
    else strcpy (Hazard, "Hazardous");
}

//  In selectionAN, Hazard must be big enough for the longest string
    char Hazard[15];
...
    chkPSI (PSI, Hazard);  // Pass the string array as an argument
...
    printf("%s", Hazard);  // Note change to string format 










Are you using a C++ compiler? Afterall, this is a C++ forum.


Well then that explains that, sigh, it seems that i have been using C all along.

I usually refer to this site for most problems I usually have and it normally works so that explains that. I apologies for any inconveniences caused and thank you for all the help you have provided, I finally can get it to work.

Thanks again,
Fl4me
Topic archived. No new replies allowed.