6 Replies - 1993 Views - Last Post: 15 January 2009 - 11:17 PM Rate Topic: -----

#1 Smurphy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

Access violation

Posted 15 January 2009 - 01:15 PM

Ok well once again back to the C/ C++ forums as I expect I will be often since I just started a C class. Anyway I wrote this dum little game that just takes four numbers and if you pick the right one you take over the world.
#include <stdio.h>
#include <stdlib.h>
int main() 
{
	int launchcode = rand() %25+1;
	int code1;
	int code2;
	int code3;
	int code4;
	char buffer[20];
	
	printf(" Hello commandor please input your first name only\n");
	
	scanf("%s", &buffer);
	
	printf("Access granted welcome commander %s\n Please enter four numbers between 1 and 25 these are your launch codes.\n If they match the world is yours\n ", &buffer);
	
	printf("First number please\n");
	scanf("%d",&code1);
	
	if (code1 == launchcode)
	{
			 printf("The world is yours\n");
			 goto programend;
			 }
			 else 
			 if(code1 != launchcode)
			 {
					  printf("Launch code 1 failed\n");
					  }
	
	printf("Second number\n");
	scanf("%d", &code2);
	 if(code2 == launchcode)
	{
			 printf("The world is yours\n");
			 goto programend;
			 }
			 else 
			 if(code2 != launchcode)
			 {
					  printf("Launch code 2 failed\n");
					  }
	
	printf("Third number\n");
	scanf("%d", &code3);
	 if(code3 == launchcode)
	{
			 printf("The world is yours\n");
			 goto programend;
			 }
			 else 
			 if(code3 != launchcode)
			 {
					  printf("Launch code 3 failed\n");
					  }
	
	printf("I know your happy its the last one fourth number please\n");
	scanf("%d", code4);
	 if(code4 == launchcode)
	{
			 printf("The world is yours\n");
			 goto programend;
			 }
			 else 
			 if(code4 != launchcode)
			 {
					  printf("Launch code 4 failed\n");
					  }
	
	printf("The launch code was %d\n", launchcode); 
	system("Pause");
	return 0;
	
	programend:
			   printf("All your launch codes have failed you are not the true commander. You fail");
			   system("Pause");
}



This is giving me a access violation error from my debugger I really have no idea whats wrong and for the most part it works.

Is This A Good Question/Topic? 0
  • +

Replies To: Access violation

#2 matthew180  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

Re: Access violation

Posted 15 January 2009 - 02:35 PM

Not sure, for the most part *what* you are going looks okay (the way you are doing it could be cleaner, IMHO). Haha, "goto", wow. I'll be surprised if someone else here does not gripe at you about that. You should really try to avoid goto though, even in BASIC...

Anyway, did your debugger give you any line numbers associated with the access violations? The only syntax changes I can recommend you make are your use of the variable "buffer". It is already a pointer, so you don't need to use the address-of operator (&) on it in your scanf and printf.

scanf("%s", &buffer);

// should be
scanf("%s", buffer);



Same with the printf right below that, drop the &buffer and just use buffer.

Matthew

This post has been edited by matthew180: 15 January 2009 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#3 Smurphy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

Re: Access violation

Posted 15 January 2009 - 03:30 PM

Thank you ya not really used to the whole C thing yet. Since most of my knowledge is in Javascript. OK Also ya about the goto thing I know I know that was really lazy of me but anyway ok no more & after a pointer.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Re: Access violation

Posted 15 January 2009 - 04:11 PM

Here you go:
scanf("%d", code4);

should be
scanf("%d", &code4);

Was This Post Helpful? 1
  • +
  • -

#5 achal3754  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-January 09

Re: Access violation

Posted 15 January 2009 - 09:40 PM

You are getting access violation because you are using
scanf("%d", code4);
as scanf need an address where it can put data what it read from console.
Since you are not using & before code4 then it gets invalid address.Hence access violation.
Use like this
scanf("%d", &code4);
Was This Post Helpful? 0
  • +
  • -

#6 abraham2119  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-January 09

Re: Access violation

Posted 15 January 2009 - 09:44 PM

@'system("PAUSE")': http://www.gidnetwork.com/b-61.html
Was This Post Helpful? 0
  • +
  • -

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Access violation

Posted 15 January 2009 - 11:17 PM

You are getting an access violation because of this line:
scanf("%d", code4);

Since you are passing the value of code4 to scanf() and not its address, the value (zero in this case) is considered as the address where scanf will write the input data.

Since writing at address 0x0 is not permitted, you get the access violation error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1