Help with C (new)
I just started taking C classes this past semester and I am having trouble with one of the homework problems I was assigned. Of course I have made an attempt at it but to no avail.
I am currently using Visual Studio 2008 Beta and when do "Build Solution" no errors come up at all. When I run the program, the first line prints as it should but as soon as I enter anything, it crashes.
The point of the homework was to design a program where a person can enter the account number, current balance, the amount charged, credits available, maximum credits, and it should print the final balance after all that and tell you if the consumer had exceeded the credit limit or not.
Below is what i have so far and any help is greatly appreciated.
// 3.18.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
/*
Name: Brian Jung
Course: COP 2220
File: 3.18
Purpose: Experiment with interactive input
*/
int main()
{
int account;
float balance;
float charge;
int credits;
float creditLimit;
float balanceAll;
account = 1;
balance = 0;
charge = 0;
credits = 0;
creditLimit = 0;
balanceAll = 0;
printf(" Enter account number (-1 to end): ");
scanf_s("%d", account);
printf("\n Enter beginning balance: ");
scanf_s("%f", balance);
printf("\n Enter total charges: ");
scanf_s("%f", charge);
printf("\n Enter total credits: ");
scanf_s("%d", credits);
printf("\n Enter credit limit: ");
scanf_s("%f", creditLimit);
while ( account >= 1 )
{
if ( creditLimit < balanceAll )
{
printf(" Account Number: \n", account);
printf(" Credit Limit: \n", credits);
balanceAll = balance + charge - credits;
printf(" Balance: \n", balanceAll);
}
else
{
printf(" Account Number: \n", account);
printf(" Credit Limit: \n", credits);
printf(" Balance: \n", balanceAll);
printf(" Credit Limit Exceeded \n");
}
}
return 0;
}
[2151 byte] By [
mrcizzo] at [2007-11-20 10:40:09]

# 1 Re: Help with C (new)
when do "Build Solution" no errors come up at allAlso no warnings?? If that is true, check where to enable warnings in your compiler or use a different compiler.
I get a warning "format argument is not a pointer (arg 2)" for every occurence of scanf in your program. Read again in your class notes, how scanf is to be used.
NB: Is this a C or a C++ class? If it is C, your file should have extension .c, not .cpp. If this is C++, you shouldn't use stdio.h/printf/scanf but iostream/<</>>.
# 2 Re: Help with C (new)
My teacher wants us to use the C++ project template in Visual Studio rather than the C. I never bothered to ask why considering he is the teacher.
And in Visual Studio 2008, whenever I attempt to use just regular "scanf" I get a ton of warnings saying it's not a secure method of doing so which is why I changed it to the suggested "scanf_s"
My friend says he got errors when I sent him my code since he uses VS2005. Could it be something wrong with VS2008?
EDIT:
No warnings come up either.
# 3 Re: Help with C (new)
And in Visual Studio 2008, whenever I attempt to use just regular "scanf" I get a ton of warnings saying it's not a secure method of doing so which is why I changed it to the suggested "scanf_s".So it gives you a warning about security, but no warning about format mismatch. To put it clear again, all arguments to scanf/scanf_s except for the first one, should be pointers to variables, not the variables themselves.
Also your printf statements are erroneous, as the strings are missing a format specifier (i.e. %d/%f).
# 4 Re: Help with C (new)
With your help I fixed part of it! I went back to the first chapter and saw I was forgetting the & sign before my variable names.
But now after I input all those the while loop goes crazy and loops like a million times over instead of looping back to the account number.
EDIT:
After doing some research I know where my mistake for the while loop is but I can't think of a condition that would allow it to restart. I was thinking of actually making it say "account = -1" at the end of the loop but then the loop won't restart.
# 5 Re: Help with C (new)
With your help I fixed part of it! I went back to the first chapter and saw I was forgetting the & sign before my variable names.
But now after I input all those the while loop goes crazy and loops like a million times over instead of looping back to the account number.
There's nothing in the loop in the code in your first post that changes account, so there's no reason for the loop to end.
GCDEF at 2007-11-9 1:28:26 >

# 6 Re: Help with C (new)
There's nothing in the loop in the code in your first post that changes account, so there's no reason for the loop to end.
Yeah after digging through the book I noticed I had noticed that too. If I were to add a line that said "account = -1" it would definitely end the loop but it won't bring it back to the first prompt.
# 7 Re: Help with C (new)
Yeah after digging through the book I noticed I had noticed that too. If I were to add a line that said "account = -1" it would definitely end the loop but it won't bring it back to the first prompt.
Seems like you want the prompts inside the loop then.
GCDEF at 2007-11-9 1:30:21 >

# 8 Re: Help with C (new)
Seems like you want the prompts inside the loop then.
So I fixed the loop problem with your suggestion. Much thanks.
Now one last one that bugger me.
It is supposed to print the results after the user inputs them and I look at the book and the code matches. But the inputs don't display.
EDIT:
Fixed the printf issue but the -1 thing doesn't want to work.
UPDATE:
// 3.18.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
/*
Name: Brian Jung
Course: COP 2220
File: 3.18
Purpose: Experiment with interactive input
*/
int main()
{
int account;
int counter;
float balance;
float charge;
int credits;
float creditLimit;
float balanceAll;
account = 1;
balance = 0;
charge = 0;
credits = 0;
creditLimit = 0;
balanceAll =0;
counter = 1;
while ( counter > -1 )
{
printf(" Enter account number (-1 to end): ");
scanf_s("%d", &account);
printf("\n Enter beginning balance: ");
scanf_s("%f", &balance);
printf("\n Enter total charges: ");
scanf_s("%f", &charge);
printf("\n Enter total credits: ");
scanf_s("%d", &credits);
printf("\n Enter credit limit: ");
scanf_s("%f", &creditLimit);
if ( creditLimit < balanceAll )
{
printf(" Account Number: %d\n", account);
printf(" Credit Limit: %d\n", credits);
balanceAll = balance + charge - credits;
printf(" Balance: %f\n\n", balanceAll);
}
else
{
printf(" Account Number: %d\n", account);
printf(" Credit Limit: %d\n", credits);
printf(" Balance: %f\n", balanceAll);
printf(" Credit Limit Exceeded \n\n");
counter = -1;
}
}
return 0;
}
# 9 Re: Help with C (new)
If I read this
printf(" Enter account number (-1 to end): ");
scanf_s("%d", &account);
I would think that your loop condition should be
while ( account > -1 )
But then that's what you started with and you decide how your program is supposed to work
Kurt
ZuK at 2007-11-9 1:32:24 >
