# Question

Page 1 of 1

## 4 Replies - 549 Views - Last Post: 21 May 2010 - 12:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=174601&amp;s=0a3ee0f21554e9165359d2222a4b7b94&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tony03

Reputation: 0
• Posts: 9
• Joined: 21-May 10

# Question

Posted 21 May 2010 - 10:24 AM

Hi everyone,

I just start to learn c++.
I have a code to determine what is mean for x when x is a certain number. However, I can't show the result of it. Can someone check for my code, please!

Thanks

```#include <iostream>

using namespace std;

int main()
{
int x;
char result,W,S,Nothing;

start:
{
printf("x=");
scanf("%i",&x);

if (x>=60 && x<=100) //if the range is larger than or equal to 60, showed "W".
result=W;

else if (x<=-40 && x<=-100) //if the range is smaller than -40, showed "S".
result=S;

else //(x<=59&&x>=-39) //if x is in the range between -39 to 59, showed "Nothing".
result=Nothing;

printf("result= \n\n", result);
goto start;

return 0;
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Question

### #2 BetaWar

• #include "soul.h"

Reputation: 1369
• Posts: 7,873
• Joined: 07-September 06

## Re: Question

Posted 21 May 2010 - 10:27 AM

This line:
```printf("result= \n\n", result);
```

Needs to be formatted like so:
```printf("result=%c \n\n", result);
```

THe %c tells printf that there will be a character that we want to insert at that location.

Hope that helps.

• Saucy!

Reputation: 6236
• Posts: 24,001
• Joined: 23-August 08

## Re: Question

Posted 21 May 2010 - 10:37 AM

There are just so many things wrong here...goto, C I/O instead of C++ I/O, logic errors.

You've declared a bunch of variables here:
```char result,W,S,Nothing;
```

which have no set values and you then use willy-nilly without any idea what you're doing. If you're trying to use strings, then char is not the type you want; you want const char *s, like this:

```const char *W = "W", *S = "S", *Nothing = "Nothing";
const char *result = Nothing;
```

Then printed using
```printf("result=%s \n\n", result);
```

s for string.

### #4 taylorc8

• B&

Reputation: 150
• Posts: 1,572
• Joined: 21-July 09

## Re: Question

Posted 21 May 2010 - 11:56 AM

Start using the string class early:

```#include <iostream>
#include <string>
using namespace std;

int main()
{

int X=0;//<---- initialize that to zero!
string result="Result";
string W="W";
string S="S";

//dont use char* 's for strings!
//use const char* !

cout << "X=" << X << endl;//<<-- the "endl" inserts a newline.
cin >> X; //<-- wait for the user to enter a value for X

.
.

return 0;
}
```

Oh, and stop putting random '{' and '}' in your code too.

This post has been edited by taylorc8: 21 May 2010 - 12:04 PM

### #5 tony03

Reputation: 0
• Posts: 9
• Joined: 21-May 10

## Re: Question

Posted 21 May 2010 - 12:51 PM

Thank you everyone!!
I just a newbie on c++ and learn a lots now

Finally, the program works fine!!

This post has been edited by tony03: 21 May 2010 - 01:17 PM