How can i return a char with array in function to main?

char* category (int aqi)
{
char *p;
if(aqi>=0 && aqi<=50)
p="Good";
else if(aqi>=51 && aqi<=100)
p="Moderate";
else if(aqi>=101 && aqi<=200)
p="Unhealthy";
else if(aqi>=201)
p="Hazarhous;
else
p="Nothing";
return p;
}
Last edited on
the output didnt show any character,how to solve it?
There is no output, because nothing is output. Showing actual code that you know compiles and reproduces the problem you're experiencing is advised -- this won't compile.
#include<stdio.h>
char category(int aqi);
int main()
{
char aqr[20];
//enter aqr
aqr[20] = category(aqi);
return 0;
}
char category(int aqi)
{
char x[20];
if(aqi>300 && aqi<401)
x[20]= 'Good';
else if(aqi>401)
x[20]='Unhealthy';
else
x[20]='Nothing';
return x;
}



base on the code on above,is it right to return a char with array to main or how to using strcpy to do it?or how to slove it?i nid to return the status to main

Last edited on
base on the code on above,is it right to return a char with array to main

As shown above, no it is not correct. First you declared your function to return a single character, not an array. Second even if you did declare your function to return a char* you can't return an array that is local to your function. Third you really should study up on Arrays and Character Sequences. Start with these two links: http://www.cplusplus.com/doc/tutorial/arrays/ and http://www.cplusplus.com/doc/tutorial/arrays/ and I also suggest you study up a little on Pointers: http://www.cplusplus.com/doc/tutorial/pointers/


But you really should learn to use the std::string class instead of the C-strings, it will make your life much easier.

Something like the following may work.
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
#include <iostream>

using namespace std;

const char* category (int aqi);

int main()
{
   const char* test;
   test = category(60) ;
   std::cout << test << std::endl;

}
const char* category (int aqi)
{
   const char *p;
   if(aqi>=0 && aqi<=50)
      p="Good";
   else if(aqi>=51 && aqi<=100)
      p="Moderate";
   else if(aqi>=101 && aqi<=200)
      p="Unhealthy";
   else if(aqi>=201)
      p="Hazarhous";
   else
      p="Nothing";
   return p;
}
Last edited on
can u show me how to return an array from a function to main,i m a student now,only start to learn c++.
but now i hvt learn about std::string,my school only teach the basic one.can u show me a basic code that can return a char with an array?
The best way to "return" and array from a function would be to pass that array to your your function as a parameter. Or even better yet use the standard string class.

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
void category (char *test, int aqi);
string category(int aqi);

int main()
{
   char test[100];
   category(test, 60);
   cout << test << endl;

   string mstring = category(60);
   cout << mstring << endl;
   return(0);
}

void category (char *test, int aqi)
{
   strcpy(test,"Moderate");
}

string category(int aqi)
{
   string retVal = "Moderate";

   return retVal;
}


Last edited on
Because this is my assignment,so the question mention that must follow:
o Parameter : int aqi
o Return type : string
o Determine air quality result based on the following table:

If that, i can use the code like your reply on above?
If that, i can use the code like your reply on above?

I would say yes, if your definition of string is to use std::string, the following returns a string from the function.


1
2
3
4
5
6
string category(int aqi)
{
   string retVal = "Moderate";

   return retVal;
}


You may want to read up on the string class. http://www.cplusplus.com/reference/string/string/string/
Last edited on
Are your string refer to any means or variables ?Because if i follow your code on above,i get syntax error there.
Last edited on
Your question doesn't really make sense. A "string" is a standard C++ class. Please study the link I provided above.

Ok, thx ya!
Topic archived. No new replies allowed.