This is an archive of a previous version of Sodarace.

Please visit http://sodarace.net for the latest version.

forum  |  »» sodaplay
 
»» forums  »» community discussion forum

subject: Do you want to learn to program in C

357 replies on 24 pages. most recent reply: Tues, Aug 28 10:58 PM by matthew102000

»» back to topic list  

This topic has 357 replies on 24 pages [ « | 1 ... 3 4 5 6 7 8 9 10 11 ... 24  | » ]
»» previous topic   »» next topic  


mmaarrkkuuss

»» models
»» homepage

Re: Do you want to learn to program in C   posted: 23-Mar-07 17:54   »» 
STDP

I can't make this work:P I've debugged it but I can't fix it:P

#include<stdio.h>

int main (void)

{
int masses, count;
float distance, current_loc;
char name[15];
FILE *fp;

printf("Name of new file: ");
scanf("%s", name);

fp = fopen( name, "w");

do
{
printf("Number of masses: ");
scanf("%d", &masses);
} while ( masses <=1);

do
{
printf("Distance between masses: ");
scanf("%f", &distance);
} while ( distance < 10);

for ( count = 0; count < masses; count++)
{
fprintf( fp,"<mass id=\"m%d\" x=\"%f\" y=\"10.0\" vx=\"0.0\" vy=\"0.0\" />\n",count,current_loc);

current_loc = current loc + distance;
}

for ( count = 0; count < masses - 1; count++)
{
frintf(fp,"<spring a=\"m%d\" b=\" m%d\" restlenght=\"%f\" />\n",count, count+1,distance);
}
fclose(fp);

printf("File successfully created. ");

scanf("%c");

return 0;
}

I would be glad if you would help:)

~mmaarrkkuuss~

cactus1

»» models
»» homepage

Re: Do you want to learn to program in C   posted: 23-Mar-07 18:03   »» 
#include<stdio.h>

int main (void)

{
int masses, count;
float distance, current_loc;
char name[15];
FILE *fp;

printf("Name of new file: ");
scanf("%s", name);

fp = fopen( name, "w");

do
{
printf("Number of masses: ");
scanf("%d", &masses);
} while ( masses <=1);

do
{
printf("Distance between masses: ");
scanf("%f", &distance);
} while ( distance < 10);

for ( count = 0; count < masses; count++)
{
fprintf( fp,"<mass id=\"m%d\" x=\"%f\" y=\"10.0\" vx=\"0.0\" vy=\"0.0\" />\n",count,current_loc);

current_loc = current loc + distance;
}

for ( count = 0; count < masses; count++)
{
fprintf(fp,"<spring a=\"m%d\" b=\" m%d\" restlength=\"%f\" />\n",count, count+1,distance);
}
fclose(fp);

printf("File successfully created. ");

scanf("%c");

return 0;
}

cactus1

»» models
»» homepage

Re: Do you want to learn to program in C   posted: 23-Mar-07 18:05   »» 
stdp. Messed up on one detail

#include<stdio.h>

int main (void)

{
int masses, count;
float distance, current_loc;
char name[15];
FILE *fp;

printf("Name of new file: ");
scanf("%s", name);

fp = fopen( name, "w");

do
{
printf("Number of masses: ");
scanf("%d", &masses);
} while ( masses <=1);

do
{
printf("Distance between masses: ");
scanf("%f", &distance);
} while ( distance < 10);

for ( count = 0; count < masses; count++)
{
fprintf( fp,"<mass id=\"m%d\" x=\"%f\" y=\"10.0\" vx=\"0.0\" vy=\"0.0\" />\n",count,current_loc);

current_loc = current loc + distance;
}

for ( count = 0; count < masses-1; count++)
{
fprintf(fp,"<spring a=\"m%d\" b=\" m%d\" restlength=\"%f\" />\n",count, count+1,distance);
}
fclose(fp);

printf("File successfully created. ");

scanf("%c");

return 0;
}

mmaarrkkuuss

»» models
»» homepage

Re: Do you want to learn to program in C   posted: 23-Mar-07 18:11   »» 
Thanks! But it still says that there's something wrong with "while ( distance < 10);" at line 26 :/

~mmaarrkkuuss~

cactus1

»» models
»» homepage

Re: Do you want to learn to program in C   posted: 23-Mar-07 20:06   »» 
Test it now.

#include<stdio.h>

int main (void)

{
int masses, count;
float distance, current_loc;
char name[15];
FILE *fp;

printf("Name of new file: ");
scanf("%s", name);

fp = fopen( name, "w");

do
{
printf("Number of masses: ");
scanf("%d", &masses);
} while ( masses <=1);

do
{
printf("Distance between masses: ");
scanf("%f", &distance);
} while ( distance < (float)10);

for ( count = 0; count < masses; count++)
{
fprintf( fp,"<mass id=\"m%d\" x=\"%f\" y=\"10.0\" vx=\"0.0\" vy=\"0.0\" />\n",count,current_loc);

current_loc = current loc + distance;
}

for ( count = 0; count < masses-1; count++)
{
fprintf(fp,"<spring a=\"m%d\" b=\" m%d\" restlength=\"%f\" />\n",count, count+1,distance);
}
fclose(fp);

printf("File successfully created. ");

scanf("%c");

return 0;
}

mmaarrkkuuss

»» models
»» homepage

Re: Do you want to learn to program in C   posted: 23-Mar-07 20:39   »» 
Thanks! Now it has no bugs, but when I create a file and upload it to the constructor, it is empty :'( Sorry if this is a little annoying:P

~mmaarrkkuuss~

cactus1

»» models
»» homepage

Re: Do you want to learn to program in C   posted: 24-Mar-07 01:25   »» 
That's because you don't have all the stuff there. You forgot the environment tags as well as <nodes> and <links>.

matthew102000

»» models

Re: Do you want to learn to program in C   posted: 24-Mar-07 01:53   »» 
i found out something cool. if you want to tab over your text (printf(text)) to make it mote organized, use \t. it works like the \n function, but instead of making a new line, it tabs over the text.

matt wuz here.

oh. ps, i still dont get how to make files with the c program. dail, please help me. whatever i do, it ends up wrong. and also, i dont get how to make things equil oyher things, so later i can type something, and whatever the yser imput is will be put there. like i said. please help.

matt wuz here again.

matthew102000

»» models

Re: Do you want to learn to program in C   posted: 24-Mar-07 01:55   »» 
sorry for my bad spelling on that last post.

matt wuz here

matthew102000

»» models

Re: Do you want to learn to program in C   posted: 24-Mar-07 02:06   »» 
dail, i just read your 2nd edition post. i would lile it. even thogh im still struggleing to understand the first one, i have the conceps down, i just nees the punctuation and stuff down. i know why they work, but im bad at making them work, if you know what im saying. anyway, im interested.

matt wuz here

cactus1

»» models
»» homepage

Re: Do you want to learn to program in C   posted: 24-Mar-07 11:46   »» 
To make things equal, you have to cast them. For example:
int a=0;
float q;
q=(float)a;

Converts variable a into a floating point variable and stores it in q.

cactus1

»» models
»» homepage

Re: Do you want to learn to program in C   posted: 24-Mar-07 11:47   »» 
stdp

For osme things, it is generally not suggested to cast. For example, you probably don't want to cast an integer into a character.

mmaarrkkuuss

»» models
»» homepage

Re: Do you want to learn to program in C   posted: 24-Mar-07 13:21   »» 
I have tried to place in the other stuff in the code but it did not work:( It has no bugs, it's just that when I try to import the file sodaconstructor stops working:P

#include<stdio.h>

int main (void)

{
int masses, count;
float distance, current_loc;
char name[15];
FILE *fp;

printf("Name of new file: ");
scanf("%s", name);

fp = fopen( name, "w");

do
{
printf("Number of masses: ");
scanf("%d", &masses);
} while ( masses <=1);

do
{
printf("Distance between masses: ");
scanf("%f", &distance);
} while ( distance < (float)10);

fprintf( fp, "<\?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"\?> \n"
"<!DOCTYPE sodaconstructor>\n"
"<model>\n"
"\t<comment></comment>\n"
"\t<container width=\"651\" height=\"422\" />\n"
"\t<environment gravity=\"0.25469473\" friction=\"0.042274434\" springyness=\"0.3537427\" />\n"
"\t<collisions surface_friction=\"0.1\" surface_reflection=\"-0.75\" />\n"
"\t<wave amplitude=\"0.76744187\" phase=\"0.67803925\" speed=\"0.014117647\" />\n"
"\t<settings gravitydirection=\"down\" wavedirection=\"reverse\" autoreverse=\"on\" />\n"
"<nodes>\n");



for ( count = 0; count < masses; count++)
{

fprintf( fp,"<mass id=\"m%d\" x=\"%f\" y=\"10.0\" vx=\"0.0\" vy=\"0.0\" />\n",count,current_loc);

current_loc = current_loc + distance;
}

fprintf( fp, "</nodes>\n");
fprintf( fp, "\t<links>\n");

for ( count = 0; count < masses-1; count++)
{

fprintf(fp,"<spring a=\"m%d\" b=\" m%d\" restlength=\"%f\" />\n",count, count+1,distance);
}

fputs("\t</links>\n", fp);
fputs("\t</model>\n", fp);

fclose(fp);

printf("File successfully created. ");

scanf("%c");

return 0;
}


I'm getting better:\ Thanks for the help:)

~mmaarrkkuuss~

matthew102000

»» models

Re: Do you want to learn to program in C   posted: 24-Mar-07 22:33   »» 
cactus1, i think what i ment to say was when the user imputs something, i cant figure out how to put that in the file that is created. how would i go about doing so? please help.

matt wuz here

cactus1

»» models
»» homepage

Re: Do you want to learn to program in C   posted: 25-Mar-07 12:40   »» 
You use fputs and fprintf, after opening the file. You might be able to use a different mode setting other than "w", such as "a", the keep the original contents and just add to the end of the file.

Also, here is a good C++ reference site.
http://www.cplusplus.com/fopen



This topic has 357 replies on 24 pages [ « | 1 ... 3 4 5 6 7 8 9 10 11 ... 24  | » ]


»» previous topic   »» next topic  

»» back to topic list  »» top of the page  

PLEASE READ THE FORUM GUIDELINES AND ALWAYS PREVIEW TO CHECK MESSAGES BEFORE POSTING...
...Help keep our forums creative and constructive. Thank you.


»» forum home