Results 1 to 8 of 8

Thread: Help...`

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Seattle
    Posts
    954
    What is wqrong with the following code, it works but I get two error when compiling.

    // ws2.cpp : Defines the entry point for the console application.
    //

    //include header files
    #include "stdafx.h"
    #include "stdio.h"

    int main(void)
    {
    //declare variables
    int status;
    float i;
    char y;
    //set y = to y
    y=y;
    //start while loop, only while = y
    while(y)
    {
    //use common printf function
    printf("Please enter the number of Pounds: ");
    //scan the amount entered
    scanf("%f",&i);
    //calculate and display the amount
    printf("The amount of Dollars is: %.2f\n ", i*1.5);
    //ask of they want to continue
    printf("Do you want to continue?(Y/N)");
    //get input
    scanf("%c",&y);
    y=getchar();
    //if anything but y, stop function
    if (y!='y')break;
    }

    return 0;
    }



    Also if I want to check to make sure that they have input a number when asked about how many pounds they want to convert, how would I do that?

  2. #2
    Addicted Member
    Join Date
    Nov 2000
    Posts
    143
    -not to good at C but I know your last part is wrong.
    Code:
    if (y!='y')break;
    should be
    Code:
    if  (y!=='y') break;
    = means assigns
    == means equal.
    Maybee you knew that maybee not.




  3. #3
    Addicted Member
    Join Date
    Nov 2000
    Posts
    143
    "Also if I want to check to make sure that they have input a number when asked about how many pounds they want to convert, how would I do that?"

    This is one way to do it.
    Untested!!
    Code:
    {
    if ( i < 48 || i > 57 )
       its not a number
    else
       its a number ( your code here ) 
    }
    Hope this helps

  4. #4
    Guest
    Originally posted by Help
    -not to good at C but I know your last part is wrong.
    Code:
    if (y!='y')break;
    should be
    Code:
    if  (y!=='y') break;
    = means assigns
    == means equal.
    Maybee you knew that maybee not.




    he was correct,

    Code:
    //.....
    if (y=5) cout << "true";
    that always evaluates to true, well because you are assigning 5 to y, and assuming that goes well, it returns true, meaning y now holds the value 5.

    Code:
    //.....
    if (y == 5) cout << "true";
    if y was equal to five, the output would be true, if not, well, there would be no output.

    Code:
    if (y != 5) cout << "false";
    if y was equal to five, that would not evaluate to true, it is asking if y is not equal to 5,

    this code accomplishes the same thing:

    Code:
    if (!(y == 5)) cout << "false";
    obviously the uppermost code is easier to read and understand. this code is saying "if (not) y equals 5" the other is saying "if y does not equal 5"


    what you are doing,

    Code:
    if (y !== 5) cout << "true"
    well, that will probably give an error, becase not equals is !=, NOT !==.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    stdio.h should be in angle-brackets, since it's a system include file.

    Also...why do you need stdafx.h?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Guest
    it's a file that VC++6 produces(I am not sure if 5 does too)

    it's not really needed, I think all it defines is win32_lean_and_mean

    this is what was included in a console app that I made:

    Code:
    // stdafx.h : include file for standard system include files,
    //  or project specific include files that are used frequently, but
    //      are changed infrequently
    //
    
    #if !defined(AFX_STDAFX_H__429D4763_E6C0_11D4_A856_0010B509E4D8__INCLUDED_)
    #define AFX_STDAFX_H__429D4763_E6C0_11D4_A856_0010B509E4D8__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    #define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
    
    #include <stdio.h>
    
    // TODO: reference additional headers your program requires here
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_STDAFX_H__429D4763_E6C0_11D4_A856_0010B509E4D8__INCLUDED_)
    usualy I just choose empty project and add new files.... I think the stdafx files are a pain in the ass....

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    5 doesn't produce it if you just make a new blank project. I code everything from scratch anyway, so I can't tell.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Guest
    yeah me too.. I think it's a pain to have to have those stdafx files in there.. once I accidently forgot to choose "blank project" and it added those files.... when I tried to compile, it said the files were missing when they weren't even needed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width