Re: Learning C - Scanf or Getch, or Getchar not working correctly after first loop.



"tesh.uk@xxxxxxxxxxxxxx" wrote:

I have written the following code with the help of Ivor Horton's
Beginning C :

// Structures, Arrays of Structures.

#include "stdafx.h"
#include "stdio.h"
#define MY_ARRAY 15
#define BASIS 360

struct swap
{
char counterParty[10];
float notional;
float intRate;
int busDays;
//int basis = 360;
float flows;
};

void main()
{

/*Arrays of Structures*/

swap myArraySwap[MY_ARRAY];

int hcount = 0;
int i = 0;
//='\0';

// for (hcount = 0; hcount < 5 ; hcount++ )
do
{
char test = NULL;
printf("\nWould you like to enter Swaps (Y or N)?:");

test = getchar();

if (test != 'Y') //|| test !='y')
break;

printf("\nEnter CounterParty:");
scanf("%s", &myArraySwap[hcount].counterParty );
printf("\nEnter Notional :" );
scanf("%f", &myArraySwap[hcount].notional );
printf("\nEnter Interest Rate:" );
scanf("%f", &myArraySwap[hcount].intRate );
printf("\nEnter Number of days:" );
scanf("%d", &myArraySwap[hcount].busDays );
myArraySwap[hcount].flows = (myArraySwap[hcount].notional *
(myArraySwap[hcount].intRate/100.0f) *
((float)myArraySwap[hcount].busDays/360.0f));

hcount++;
}
while (hcount < 5);

for (i=0 ; i < hcount ; i++)
{
printf("\nCounter Party: %s",myArraySwap[hcount].counterParty);
printf("\nFlows %f", myArraySwap[i].flows);
printf("\nDone");
}

}

Errors detected:

[1] c:\c\junk>cc junk.c
junk.c:1:20: stdafx.h: No such file or directory (ENOENT)
junk.c:12: parse error before '/' token
junk.c:12: warning: no semicolon at end of struct or union
junk.c:14: parse error before '}' token
junk.c:14: warning: ISO C does not allow extra `;' outside of a
functio
junk.c:17: warning: return type of `main' is not `int'
junk.c: In function `main':
junk.c:21: `swap' undeclared (first use in this function)
junk.c:21: (Each undeclared identifier is reported only once
junk.c:21: for each function it appears in.)
junk.c:21: parse error before "myArraySwap"
junk.c:26: parse error before '/' token
junk.c:28: parse error before ')' token
junk.c:34: `test' undeclared (first use in this function)
junk.c:36: parse error before '/' token
junk.c:36: warning: empty body in an if-statement
junk.c:40: `myArraySwap' undeclared (first use in this function)
junk.c:25: warning: unused variable `i'
junk.c:28: warning: statement with no effect
junk.c: At top level:
junk.c:53: parse error before "while"
junk.c:58: parse error before string constant
junk.c:58: warning: type defaults to `int' in declaration of
`printf'
junk.c:58: warning: conflicting types for built-in function
`printf'
junk.c:58: ISO C forbids data definition with no type or storage
class
junk.c:59: parse error before string constant
junk.c:59: warning: type defaults to `int' in declaration of
`printf'
junk.c:59: ISO C forbids data definition with no type or storage
class

After correcting the foolish use of // comments we get:

[1] c:\c\junk>cc junk.c
junk.c:1:20: stdafx.h: No such file or directory (ENOENT)
junk.c:17: warning: return type of `main' is not `int'
junk.c: In function `main':
junk.c:21: `swap' undeclared (first use in this function)
junk.c:21: (Each undeclared identifier is reported only once
junk.c:21: for each function it appears in.)
junk.c:21: parse error before "myArraySwap"
junk.c:40: `myArraySwap' undeclared (first use in this function)

After you correct these errors so that the code is compilable in
ISO standard C, repost and we will try to help you. Read the
documentation for your system to find out how to make it compile
standard C.

I had thought that Horton was fairly sound. Looks like I may need
to revise that opinion.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews


.



Relevant Pages

  • Help with makeing DBD::DB2
    ... Constants.xs:25: warning: `not_here' defined but not used ... dbdimp.h:18: error: parse error before '*' token ... dbdimp.h:18: warning: type defaults to `int' in declaration of ...
    (comp.lang.perl.modules)
  • Re: anything wrong with this?
    ... foo.c:1: parse error before `if' ... foo.c:6: warning: type defaults to `int' in declaration of `strcpy' ...
    (comp.lang.c)
  • Parse errors
    ... client.c:51: parse error before `for' ... client.c:53: warning: type defaults to `int' in declaration of `inf' ... client.c:54: warning: type defaults to `int' in declaration of `send' ...
    (comp.os.linux.development.apps)
  • Re: ANSI C Newbie Question
    ... jacob navia wrote on 25/02/05: ... main.c:4: parse error before '{' token main.c: At top level: ... main.c:7: warning: return type defaults to `int' ... main.c:7: warning: function declaration isn't a prototype ...
    (comp.lang.c)
  • Reason for ISO ; warning
    ... ISO C does not allow extra ';' outside of a function ... int main ... Anyone care to enlighten me as to why ISO C does not allow this, ...
    (comp.lang.c)