|
-
Jan 9th, 2001, 06:18 PM
#1
Thread Starter
Fanatic Member
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?
-
Jan 9th, 2001, 07:22 PM
#2
Addicted Member
-not to good at C but I know your last part is wrong.
should be
Code:
if (y!=='y') break;
= means assigns
== means equal.
Maybee you knew that maybee not. 
-
Jan 9th, 2001, 07:47 PM
#3
Addicted Member
"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
-
Jan 9th, 2001, 09:47 PM
#4
Originally posted by Help
-not to good at C but I know your last part is wrong.
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 !==.
-
Jan 10th, 2001, 06:05 AM
#5
Monday Morning Lunatic
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
-
Jan 10th, 2001, 06:15 AM
#6
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....
-
Jan 10th, 2001, 06:19 AM
#7
Monday Morning Lunatic
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
-
Jan 10th, 2001, 02:56 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|