Re: help with C program

  • From: "Littlefield, Tyler" <compgeek13@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 20 Sep 2007 09:05:19 -0600

ouch... harsh teacher. :)
If you use vs, it'll format your code for you.
Thanks,
Tyler Littlefield.
Vertigo head coder
"My programs don't have bugs, just randomly added features."
msn: compgeek134@xxxxxxxxxxx
email: compgeek13@xxxxxxxxx
aim: st8amnd2005
skype: st8amnd127
----- Original Message ----- From: "John Miller" <n1umj@xxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, September 20, 2007 8:56 AM
Subject: Re: help with C program


When I sent in my homework, I asked the teacher in the email about that. He said we haven't covered that and I did right to leave it out. For some one who takes 5 points off because he didn't like how I positioned a couple things, I'm sure that would have killed my grade but I did file it away for when we do cover that.
I keep everything that might be good for notes later.
----- Original Message ----- From: "Littlefield, Tyler" <compgeek13@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, September 20, 2007 10:36 AM
Subject: Re: help with C program


ah. kk.
Thanks,
Tyler Littlefield.
Vertigo head coder
"My programs don't have bugs, just randomly added features."
msn: compgeek134@xxxxxxxxxxx
email: compgeek13@xxxxxxxxx
aim: st8amnd2005
skype: st8amnd127
----- Original Message ----- From: "John Miller" <n1umj@xxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, September 20, 2007 8:21 AM
Subject: Re: help with C program


I didn't not read that part about the int, void, and return. I filed that away for right now because none of the sample programs in the book or class notes had them, 1 had a return line but, all the rest had neither. Being this is for a class where the teacher said specifically not to put anything we didn't learn yet in, that's why I left those out, originally. I have fixed the errors and made a few changes that I like and did put those in for the final copy as well as a couple other things I picked up along the way. ----- Original Message ----- From: "Marlon Brandão de Sousa" <splyt.lists@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, September 20, 2007 9:52 AM
Subject: Re: help with C program


Hello,
I am whatching to this trhead a little late, but let me suggest some
simple things that will help you alone and also us in the process of
helping you out.

First rule is: read carefully what people post in response to your
questions. You already was warned to put int or void before the main
word, and some messages latter you were still pasting the code you're
trying without it. Some compilers might accept main without a return
type, but you WILL BE IN TROUBLE IF YOU KEEP IGNORING THAT.
Please keep reading, it will help you in your next lessons ..

You still didn't learn what a function is, but let me explain it
quickly. A function is a code block that can be activated always you
need it.
It is defined this way:
return type + name of function + open parentesis + parameter list +
close parentesis.

As you can notice, the code block have to return something and can
take parameters to help it acomplish this task.

The following code ilustrates how it works in a real example

#include <stdio.h>
// next line will tell the compiler that there will be one function in
this program
// This function, called add, returns a integer, and takes two
integers as its parameters
int add(int firstNumber, int secondNumber);
/*
Next line will define the main function, which is in most part
of times the first function that will be called in your program. You
don't call the main function, the OS calls it for you when you execute
it. It is, indeed, a kind of license agreement you do with the OS. The
OS will look for a function called main and call it, and you will
provide a function called main which will contain the code of your
pgrogram.
As anyother function, the main function should have a return type, the
main word (which is the function name), and a parameter list which is
optional.
Your license agreement with the OS is that the function main should
return an integer, and can have a parameter list, which is optional as
already stated before.
*/
int main(void)
{
// the function add will obiviously add two numbers and return the
result, so we declare two integers.
int firstNumber;
int secondNumber;
int result;
// now we will call the function add, this way:
result = add(firstNumber, secondNumber);
// The function added the two numbers and returned a result, which was
stored in the variable called result
printf("result is %d\n", result);
/*
as you can remeber, the function main have a integer as a return type,
so we have to do this
now, as the main function tasks are ended. The license agreement
specifies that we should return 0 if everything went ok, which seen to
be the case. The OS can use thi value returned by main to several
things, which we won't discuss here, but it is good and responsible
programming practice to let the OS know if everything went ok or not
and, if anything went wrong, let it know what happened. As everything
went ok (e.e we added two numbers and printed the results without
problems), we just return 0. This is going to be, in most part of the
times, the last thing that your program do before exiting.
*/
return 0;
}

/*
now, we are going to write the code of the add function, which is
responsible to adding two numbers and returning the result.
*/
int add(int firstNumber, int secondNumber)
{
int result; // will contain the result
result = (firstNumber + secondNumber);
// now result contains the value of this add operation, so we return it.
return result;
}

I hope it is clearer now why the main function needs to return a value
and why it needs to have a return type.

Second rule is: if your compiler is generating errors, past then
alongside with the code, so we can read the errors and quickly detect
what is going wrong with the code. You also should keep reading the
compiler errors, as they will be your main source of tipes about what
is wrong. Pasting code from e-mails can lead to strange errors, as
line breaks and blank spaces might appear where they should not.
Althoug you're right in your assumptions that c++ won't complain about
blank spaces, the compiler can't guess what you are meaning in certain
situations. Take a look at this:

int        aNumber;

There're several spaces between the keyword int which defines the type
of the variable and the name of the variable itself. The compiler will
track it without problems.

int aNu    mber;

The compiler will see the type specifier, and look for the name of the
variable after it. It is clear that what separate words are spaces, so
the compiler finds the word aNu, and will track it as the name of the
variable. Well, after this the compiler expects a coma, which is used
to separate several declarations of variables of the same type in the
same line, or for a semicolon, which will tell it that the statement
ended. But what will happen is that, after going trhough the spaces,
what the compiler will find is the mber word, which has no meanning to
it, so it will stop and complain because it has no idea about what to
do.

So take care when pasting code from any source. Make sure that every
word has no spaces in it, as if there're spaces the compiler will
handle the word as two or more words and it is very likely to generate
errors. There can be as spaces as you wish between words, not in a
unic word.

Third role is: if you are getting lost even with your teacher, go look
for a C book online, which perhaps can help you to understand things
better than your teacher can, plus our help if you get confused in the
book. I don't know if you're going with C or c++, but if you tell it I
can try to suggest good books.
hth
Marlon

2007/9/19, John Miller <n1umj@xxxxxxxxxxx>:
I followed the instructions in the compiler and fixed that. We didn't learn that part yet, but it worked, I ran the program and it's working just fine. Thanks for the help everyone. I promise I'll leave you all alone now, unless I have an answer or 2, until my next assignment when I'll probably be lost again though maybe not. I was headed the right way in this one for a while.
It's a learning process with a ton to learn.
----- Original Message -----
From: "Littlefield, Tyler" <compgeek13@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Wednesday, September 19, 2007 10:36 PM
Subject: Re: help with C program


> scanf is deprecated because of buffer overflows. you should be fine, > just
> remember that later.
> Thanks,
> Tyler Littlefield.
> Vertigo head coder
> "My programs don't have bugs, just randomly added features."
> msn: compgeek134@xxxxxxxxxxx
> email: compgeek13@xxxxxxxxx
> aim: st8amnd2005
> skype: st8amnd127
> ----- Original Message -----
> From: "John Miller" <n1umj@xxxxxxxxxxx>
> To: <programmingblind@xxxxxxxxxxxxx>
> Sent: Wednesday, September 19, 2007 8:31 PM
> Subject: Re: help with C program
>
>
>> The original I did copy from the teacher's email as well as some >> from >> other emails though I did a bit or just basing off what I have. >> Well, I
>> got rid of all the errors, now I'm getting warnings about the scanf
>> functions advising I use something else. Can I ignore this or what >> should >> I do? The teacher did say not to use anything we haven't learned >> yet >> though in looking online I did find a, what I think is much easier >> way of
>> doing that.
>> ----- Original Message -----
>> From: "Littlefield, Tyler" <compgeek13@xxxxxxxxx>
>> To: <programmingblind@xxxxxxxxxxxxx>
>> Sent: Wednesday, September 19, 2007 10:26 PM
>> Subject: Re: help with C program
>>
>>
>>> if you coppied and pasted out of an emal, it could have junk in >>> there.
>>>
>>> Thanks,
>>> Tyler Littlefield.
>>> Vertigo head coder
>>> "My programs don't have bugs, just randomly added features."
>>> msn: compgeek134@xxxxxxxxxxx
>>> email: compgeek13@xxxxxxxxx
>>> aim: st8amnd2005
>>> skype: st8amnd127
>>> ----- Original Message -----
>>> From: "John Miller" <n1umj@xxxxxxxxxxx>
>>> To: <programmingblind@xxxxxxxxxxxxx>
>>> Sent: Wednesday, September 19, 2007 8:22 PM
>>> Subject: Re: help with C program
>>>
>>>
>>>> Well, I had 102 errors, with and with out what you said but, I'm
>>>> eliminating the blank spaces now and the more I eliminate, the >>>> more >>>> errors go away when I try to compile. Does that make sense? I >>>> thought
>>>> spaces were alright.
>>>> Could I be doing something else in that process I'm not aware >>>> of? >>>> It's not perfect yet, but I lost 2/3 of the errors in getting rid >>>> of
>>>> blank spaces so far and I'm not done yet.
>>>> ----- Original Message -----
>>>> From: "Littlefield, Tyler" <compgeek13@xxxxxxxxx>
>>>> To: <programmingblind@xxxxxxxxxxxxx>
>>>> Sent: Wednesday, September 19, 2007 9:55 PM
>>>> Subject: Re: help with C program
>>>>
>>>>
>>>>> main needs int in front of it.
>>>>> also need to return.
>>>>> Thanks,
>>>>> Tyler Littlefield.
>>>>> Vertigo head coder
>>>>> "My programs don't have bugs, just randomly added features."
>>>>> msn: compgeek134@xxxxxxxxxxx
>>>>> email: compgeek13@xxxxxxxxx
>>>>> aim: st8amnd2005
>>>>> skype: st8amnd127
>>>>> ----- Original Message -----
>>>>> From: "John Miller" <n1umj@xxxxxxxxxxx>
>>>>> To: <programmingblind@xxxxxxxxxxxxx>
>>>>> Sent: Wednesday, September 19, 2007 7:04 PM
>>>>> Subject: Re: help with C program
>>>>>
>>>>>
>>>>>> OK, I've been messing around with this since, well, early >>>>>> afternoon? >>>>>> and this is what I keep coming back to but it comes up with a >>>>>> bunch >>>>>> of errors I don't understand. I'm probalby goign to change >>>>>> little >>>>>> things up a bit once I figure out what I'm doing but maybe some >>>>>> one
>>>>>> can give me a couple hints what's wrong here?
>>>>>>
>>>>>> code is:
>>>>>> #include <stdio.h>
>>>>>>
>>>>>> main ()
>>>>>>
>>>>>> {
>>>>>>
>>>>>>       int clock;      /* clock number */
>>>>>>
>>>>>>       float gross;    /* gross pay */
>>>>>>
>>>>>>       float hours;    /* hours worked */
>>>>>>
>>>>>>       float wage;     /* hourly wage */
>>>>>>
>>>>>>       /* Prompt for input values from the screen */
>>>>>>
>>>>>>    printf("enter your employee number: ");
>>>>>>
>>>>>>    scanf("%d", &clock);
>>>>>>
>>>>>>    printf("employee number %d\n", clock);
>>>>>>
>>>>>> printf("Enter number of worked hours: ");
>>>>>>
>>>>>> scanf("%f", &hours);
>>>>>>
>>>>>> printf("Enter the wage: ");
>>>>>>
>>>>>> scanf("%f", &wage);
>>>>>>
>>>>>>       /* calculate gross pay */
>>>>>>
>>>>>>       gross = wage * hours;
>>>>>>
>>>>>>       /* print out employee information to the screen */
>>>>>>
>>>>>>
>>>>>>
>>>>>> printf("This employee worked %f hours with a per-hour basis of
>>>>>>
>>>>>> %f.\n", hours, wage);
>>>>>>
>>>>>> printf(" gross pay is %f.\n", gross);
>>>>>>
>>>>>> }
>>>>>>
>>>>>>
>>>>>>
>>>>>> end code
>>>>>>
>>>>>> ----- Original Message -----
>>>>>> From: "Delaunay Christophe" <christophe.delaunay@xxxxxxxxxxx>
>>>>>> To: <programmingblind@xxxxxxxxxxxxx>
>>>>>> Sent: Wednesday, September 19, 2007 11:23 AM
>>>>>> Subject: RE: help with C program
>>>>>>
>>>>>>
>>>>>> Hi John,
>>>>>>
>>>>>> Hey, you're not so far. You've got it all indeed.
>>>>>>
>>>>>> Here I report the code from so-called "shell with hints in >>>>>> place" and >>>>>> you will notice that you really aren't so far from the >>>>>> solution.
>>>>>>
>>>>>> Here it goes:
>>>>>> ------------------
>>>>>> #include <stdio.h>
>>>>>>
>>>>>> void main() /* This "void" is necessary in C since you must >>>>>> type >>>>>> anything. The "void" keyword means "no value". In other words, >>>>>> you
>>>>>> say
>>>>>> that the main() operation doesn't return any value. */
>>>>>> {
>>>>>>       /* Declare variables */
>>>>>>       int clock;      /* clock number */
>>>>>>       float gross;    /* gross pay */
>>>>>>       float hours;    /* hours worked */
>>>>>>       float wage;     /* hourly wage */
>>>>>>
>>>>>>       /* Prompt for input values from the screen */
>>>>>>
>>>>>>                /*... use printf and scanf here*/
>>>>>> printf("Enter number of worked hours: ");
>>>>>> scanf("%f", &hours);
>>>>>> printf("Enter the wage: ");
>>>>>> scanf("%f", &wage);
>>>>>>
>>>>>>       /* calculate gross pay */
>>>>>>       gross = wage * hours;
>>>>>>
>>>>>>       /* print out employee information to the screen */
>>>>>>
>>>>>> /*... now print out to the screen using printf >>>>>> (ie,
>>>>>> print
>>>>>> the
>>>>>> value of the variables) */
>>>>>> printf("This employee worked %f hours with a per-hour basis of
>>>>>> %f.\n", hours, wage);
>>>>>> printf("Therefore, their gross pay is %f.\n", gross);
>>>>>> } /* end of main */
>>>>>> -----------------------------
>>>>>>
>>>>>> However, maybe it is not the full solution to your problem >>>>>> since, as
>>>>>> you
>>>>>> presented the problem, I can't figure out what the "clock" >>>>>> integer
>>>>>> variable is for.
>>>>>>
>>>>>> My answer is only intended to give you some sample code which >>>>>> use the >>>>>> printf() and scanf() function and which works. But, if you >>>>>> compile it
>>>>>> as
>>>>>> is, you will get a warning "Unreferenced variable clock" since, >>>>>> as I >>>>>> don't know what it is for, I never used it within my sample >>>>>> code.
>>>>>>
>>>>>> HTH, Have a nice day. ChD
>>>>>>
>>>>>> -----Original Message-----
>>>>>> From: programmingblind-bounce@xxxxxxxxxxxxx
>>>>>> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of >>>>>> John
>>>>>> Miller
>>>>>> Sent: mercredi 19 septembre 2007 16:27
>>>>>> To: programmingblind@xxxxxxxxxxxxx
>>>>>> Subject: Re: help with C program
>>>>>>
>>>>>> OK, I've done some looking and what I have is as bad a mess as >>>>>> I >>>>>> thought. I have a shell with hints which I didn't bother with, >>>>>> and >>>>>> another one with out hints I have been working with, I'll post >>>>>> both
>>>>>> so
>>>>>> people can tell what I'm trying to do and how. I know there are >>>>>> more >>>>>> than 1 way to do some of this but we were strictly told we lose >>>>>> a lot
>>>>>> of
>>>>>> credit getting ahead of ourselves. Personally, I think there >>>>>> are
>>>>>> easier
>>>>>> ways to do some of this but can't afford to lose any more >>>>>> credit than
>>>>>> my
>>>>>> lack of knowledge will cost me already. I'll post what I'm >>>>>> working on
>>>>>> first, please to easy on the complete novis.
>>>>>> I know this is a mess, I was doing things completely wrong, >>>>>> tried to
>>>>>> fix
>>>>>> it and this is the result and I think it's still wrong but >>>>>> better
>>>>>> than
>>>>>> it was, I hope.
>>>>>>
>>>>>>
/***********************************************************************
>>>>>> */
>>>>>>
>>>>>> /* */
>>>>>>
>>>>>> /* HOMEWORK: 1 - Chapter 4 */
>>>>>>
>>>>>> /* */
>>>>>>
>>>>>> /* Name: John Miller */
>>>>>>
>>>>>> /* */
>>>>>>
>>>>>> /* Class: C Programming, Cybercourse, */
>>>>>>
>>>>>> /* */
>>>>>>
>>>>>> /* Date: 9/17/20xx */
>>>>>>
>>>>>> /* */
>>>>>>
>>>>>> /* Description: Program which determines gross pay and outputs >>>>>> */
>>>>>>
>>>>>> /* a formatted answer. */
>>>>>>
>>>>>> /* */
>>>>>>
>>>>>>
/***********************************************************************
>>>>>> */
>>>>>>
>>>>>>
>>>>>>
>>>>>> #include <stdio.h>
>>>>>>
>>>>>>
>>>>>>
>>>>>> main()
>>>>>>
>>>>>> {
>>>>>>
>>>>>> /* Declare variables */
>>>>>>
>>>>>>       int a = clock;      /* clock number */
>>>>>>
>>>>>>       float b = gross pay;    /* gross pay */
>>>>>>
>>>>>>       float c =;    /* hours worked */
>>>>>>
>>>>>>       float d = 10.0;     /* hourly wage */
>>>>>>
>>>>>> /* Prompt for input values from the screen */
>>>>>>
>>>>>>  printf("Enter hours:");
>>>>>>
>>>>>> scanf("%d", &b);
>>>>>>
>>>>>> b = c * d;
>>>>>>
>>>>>> printf ( "wage is %d \n" ) ;
>>>>>>
>>>>>> /* calculate gross pay */
>>>>>>
>>>>>>
>>>>>>
>>>>>> /* print out employee information to the screen */
>>>>>>
>>>>>> }
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> now the shell with hints in place, sorry, I knwo the above is
>>>>>> probably
>>>>>> beyond horrible.
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> #include <stdio.h>
>>>>>>
>>>>>> main()
>>>>>> {
>>>>>>       /* Declare variables */
>>>>>>       int clock;      /* clock number */
>>>>>>       float gross;    /* gross pay */
>>>>>>       float hours;    /* hours worked */
>>>>>>       float wage;     /* hourly wage */
>>>>>>
>>>>>>       /* Prompt for input values from the screen */
>>>>>>
>>>>>>                ... use printf and scanf here
>>>>>>
>>>>>>       /* calculate gross pay */
>>>>>>       gross = wage * hours;
>>>>>>
>>>>>>       /* print out employee information to the screen */
>>>>>>
>>>>>> ... now print out to the screen using printf (ie, >>>>>> print
>>>>>> the
>>>>>> value of the variables)
>>>>>>
>>>>>> }
>>>>>> __________
>>>>>> View the list's information and change your settings at
>>>>>> //www.freelists.org/list/programmingblind
>>>>>>
>>>>>> __________
>>>>>> View the list's information and change your settings at
>>>>>> //www.freelists.org/list/programmingblind
>>>>>>
>>>>>
>>>>> __________
>>>>> View the list's information and change your settings at
>>>>> //www.freelists.org/list/programmingblind
>>>>>
>>>>
>>>> __________
>>>> View the list's information and change your settings at
>>>> //www.freelists.org/list/programmingblind
>>>>
>>>
>>> __________
>>> View the list's information and change your settings at
>>> //www.freelists.org/list/programmingblind
>>>
>>
>> __________
>> View the list's information and change your settings at
>> //www.freelists.org/list/programmingblind
>>
>
> __________
> View the list's information and change your settings at
> //www.freelists.org/list/programmingblind
>

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind




--
When you say "I wrote a program that crashed Windows," people just
stare at you blankly and say "Hey, I got those with the system, for
free."
Linus Torvalds
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: