4 Replies - 424 Views - Last Post: 21 May 2010 - 12:51 PM Rate Topic: -----

#1 tony03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1105
  • View blog
  • Posts: 6,918
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • 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

.
.
.//your code here

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

Was This Post Helpful? 0
  • +
  • -

#5 tony03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :punk:

Finally, the program works fine!! :clap:

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1