error with my first program

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1187 Views - Last Post: 03 July 2015 - 08:29 AM Rate Topic: -----

#1 stayhungry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-July 15

error with my first program

Posted 01 July 2015 - 06:12 AM

Hi all, i started study C and few hours ago i've wrote my first program but i've some problems.
this is the code:
 #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main () {
int *v, i, num, somma, media;
printf ("how many votes you have taken at school ?");
scanf ("%d", &num);
v= (int*)malloc(num * sizeof(int));
for (i=0; i<num; i++) {
printf("value vote number %d: ,i ");
scanf("%d", &v[i]);
}
somma=0;
for (i=0; i<num; i++) {
somma=somma+v[i];
media=somma/num;
printf ("the average is: "%d", media);
}
free(v);
if (media<6) {
printf ("You must study!");
return 0}
else {
printf ("You're a good student!");
return 0}
} 


what does this program?or better, what should do this program? the program should ask us the number of votes that for example we got at school, later it should do an average of this votes and if this average is lesser than 6, the program say us "You must study" else it say us that we are good students.
so, i've this problems, i use geany on a linux distro and when i try to compile the code geany say me "make (into directory: /home/sasa/Scrivania) make: *** No targets specified and no makefile found . Arrest .

and when i try to run it, the terminal say "./geany_run_script.sh: 5: ./geany_run_script.sh: ./prova: Permission denied


------------------
(program exited with code: 126)
Press return to continue
"
i don't know what i've missed, i checked the code for a lot of time! in an other forum, an italian forum, an user say me that there is something of wrong in the compiler but what??


Thanks for the attention!! :D/>

Is This A Good Question/Topic? 0
  • +

Replies To: error with my first program

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: error with my first program

Posted 01 July 2015 - 06:26 AM

have you the gcc compiler or similar installed?
if so did you compile the program and did you fix the errors and get it to compile OK
I suspect you attempted to run the program without fixing the compilation errors

using geany under Ubuntu 14.04 when I hit the compile button your program compiles and gives me the following errors
gcc -Wall -c "x.c" (in directory: /home/xx/Public)
x.c: In function ‘main’:
x.c:10:1: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
 printf("value vote number %d: ,i ");
 ^
x.c:17:29: warning: missing terminating " character [enabled by default]
 printf ("the average is: "%d", media);
                             ^
x.c:17:1: error: missing terminating " character
 printf ("the average is: "%d", media);
 ^
x.c:17:28: error: ‘d’ undeclared (first use in this function)
 printf ("the average is: "%d", media);
                            ^
x.c:17:28: note: each undeclared identifier is reported only once for each function it appears in
x.c:18:1: error: expected ‘)’ before ‘}’ token
 }
 ^
x.c:18:1: error: expected ‘;’ before ‘}’ token
x.c:22:9: error: expected ‘;’ before ‘}’ token
 return 0}
         ^
x.c:25:9: error: expected ‘;’ before ‘}’ token
 return 0}
         ^
Compilation failed.


did you save the files as a .c?
did you get the above error messages?
if so there is no point trying to run it - you need to fix them before you run the program

try typing gcc in a terminal - if it is not installed install it with
sudo apt_get install gcc

This post has been edited by horace: 01 July 2015 - 06:38 AM

Was This Post Helpful? 1
  • +
  • -

#3 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 615
  • Posts: 2,595
  • Joined: 13-June 14

Re: error with my first program

Posted 01 July 2015 - 09:23 AM

View Posthorace, on 01 July 2015 - 01:26 PM, said:

try typing gcc in a terminal - if it is not installed install it with
sudo apt_get install gcc


This is not going to work on distributions that aren't Debian based and the OP didn't say which they were using.
Was This Post Helpful? 0
  • +
  • -

#4 stayhungry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-July 15

Re: error with my first program

Posted 01 July 2015 - 12:46 PM

Good evening to everyone. so, i've fixed every mistakes and now the program works. this is the code now:
 #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main () {
int *v, i, num, somma, media;
printf ("How many votes have you taken at school ?");
scanf ("%d", &num);
v= (int*)malloc(num * sizeof(int));
for (i=0; i<num; i++) {
printf("vote numer " "%d" ":" ,i);
scanf("%d", &v[i]);
}
somma=0;
for (i=0; i<num; i++) {
somma=somma+v[i];
media=somma/num;
}
printf ("your average is :""%d \n" ,media);
free(v);
if (media<6) {
printf ("you must study!");
return 0;}
else {
printf ("you're a good student'");
return 0;}
} 


@horace: -yes i ve gcc but now i've also downloaded Dev C++ on windows.
- i don't know why but when i compiled on geany, it didn't give me the errors that you have. now i try again, i add some mistakes and i compile. maybe i'll get the errors that u have because i've uptaded my ubuntu version some hours ago.

anyway, thanks to everyone :)/>
Was This Post Helpful? 0
  • +
  • -

#5 stayhungry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-July 15

Re: error with my first program

Posted 01 July 2015 - 01:04 PM

edit: ( ._. )

with the same code, geany give me the same errors: "make (into directory: /home/sasa/Scrivania) make: *** No targets specified and no makefile found . Arrest .

and when i try to run it, the terminal say "./geany_run_script.sh: 5: ./geany_run_script.sh: ./prova: Permission denied


------------------
(program exited with code: 126)
Press return to continue


but why?? the code works on windows! are there somethings wrong with geany? how can i fix the problem?
@horace @ndc85430
Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: error with my first program

Posted 01 July 2015 - 01:12 PM

think there must be something wrong with your geany installation

using geany on openSUSE your latest program compiles and starts to run OK, see attached screen dump

Just built and ran the program using geany under Ubuntu 14.04
did you click Build the current file and then Run or view the current file
clicking Compile the current file only compiles but does not build the executable

if you are looking for a cross platform IDE I use Code::Blocks which runs on Windows and Linux
http://www.codeblocks.org/

Attached image(s)

  • Attached Image

This post has been edited by horace: 01 July 2015 - 01:25 PM

Was This Post Helpful? 0
  • +
  • -

#7 stayhungry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-July 15

Re: error with my first program

Posted 02 July 2015 - 12:09 PM

View Posthorace, on 01 July 2015 - 01:12 PM, said:

think there must be something wrong with your geany installation

using geany on openSUSE your latest program compiles and starts to run OK, see attached screen dump

Just built and ran the program using geany under Ubuntu 14.04
did you click Build the current file and then Run or view the current file
clicking Compile the current file only compiles but does not build the executable

if you are looking for a cross platform IDE I use Code::Blocks which runs on Windows and Linux
http://www.codeblocks.org/


i'll try to reinstall geany and i'll install code::blocks. THANK YOU!
Was This Post Helpful? 0
  • +
  • -

#8 stayhungry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-July 15

Re: error with my first program

Posted 02 July 2015 - 12:22 PM

this is what happen if i built and later run the code.

WHY?WHY?WHY?
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,664
  • Joined: 19-February 09

Re: error with my first program

Posted 02 July 2015 - 12:54 PM

Hi, you could try running ./configure see if it suggests any problems.

Error while installing geany
Was This Post Helpful? 0
  • +
  • -

#10 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: error with my first program

Posted 02 July 2015 - 10:08 PM

View Poststayhungry, on 02 July 2015 - 08:22 PM, said:

this is what happen if i built and later run the code.

WHY?WHY?WHY?

did you miss adding an attachment?
Was This Post Helpful? 0
  • +
  • -

#11 stayhungry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-July 15

Re: error with my first program

Posted 03 July 2015 - 06:08 AM

no, i miss nothing.. i think that i'll reinstall linux, maybe i'll try with ubuntu if my pc's hardware allows me
Was This Post Helpful? 0
  • +
  • -

#12 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: error with my first program

Posted 03 July 2015 - 06:22 AM

is geany still giving you the strange error messages?
the sequence I use to run the code using Ubuntu is
1. run Firefox and copy the program from your post #4
2. run geany and paste the code into the edit window
3. select Save the current file and save it to home/xx/Public/test1.c (xx is my username)
4. select Build the current file - it compiles and builds OK
5. select run or view the current file and it opens a terminal window and displays "How many votes have you taken at school ?"

does the program work on Windows OK?
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,493
  • Joined: 08-August 08

Re: error with my first program

Posted 03 July 2015 - 06:24 AM

Your first goal, before getting your code to run in any environment, should be to make it readable. That will make it easier to debug, and therefore more likely to run. Indenting, along with proper white space, helps to make it readable. Here's your code with indenting and white space, and a couple of minor changes:

int main () {
	int *v, i, num, somma, media;
	printf ("How many votes have you taken at school ?");
	scanf ("%d", &num);
	v= (int*)malloc(num * sizeof(int));

	for (i=0; i<num; i++) {
		printf("vote numer " "%d" ":" ,i);
		scanf("%d", &v[i]);
	}

	somma=0;
	for (i=0; i<num; i++) {
		somma=somma+v[i];
		media=somma/num;
	}

	printf ("your average is :""%d \n" ,media);

	free(v);
	if (media<6) {
		printf ("you must study!");
	} else {
		printf ("you're a good student'");
	}
	return 0;
}

Was This Post Helpful? 2
  • +
  • -

#14 stayhungry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-July 15

Re: error with my first program

Posted 03 July 2015 - 06:42 AM

View Posthorace, on 03 July 2015 - 06:22 AM, said:

is geany still giving you the strange error messages?
the sequence I use to run the code using Ubuntu is
1. run Firefox and copy the program from your post #4
2. run geany and paste the code into the edit window
3. select Save the current file and save it to home/xx/Public/test1.c (xx is my username)
4. select Build the current file - it compiles and builds OK
5. select run or view the current file and it opens a terminal window and displays "How many votes have you taken at school ?"

does the program work on Windows OK?


so:
1- yes, geany is giving me the strange error messages. i press MAIUSC-F9 (to compile) and later i press F5 for run the programm. also code::blocks is giving me error messages (look at pic)
2- yes, on windows the program work perfectly

this is the pic about code::BLOCKS

(sorry for 3 posts)

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,493
  • Joined: 08-August 08

Re: error with my first program

Posted 03 July 2015 - 06:49 AM

Try removing #include <malloc.h>
I don't think it's necessary, and on my system it causes an error.

By the way, media=somma/num; should go after the for loop.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2