DS learns C

Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
I am taking a C Programming class in College and I thought I would get some advise as I go through all of this.
Sub has helped me with a few things already.

Calculator: http://www.sendspace.com/file/saho29
Code:
{
	//Floating Numbers.
	float x, y, z;

	//These are the definitions of the floaters. Sub said these can be defined above in float as well.
	x= 0.0;
	y= 0.0;
	z= 0.0;

	//Input Value for X and Y.
	printf("\nInput x=");
	scanf("%f", &x);
	printf("\nInput y=");
	scanf("%f", &y);

	//Equation
	z= x * y;

	//Prints what is in red.
	printf("\nThe Answer is...%f\n", z);
	//Pauses the console.
	system("PAUSE");
	return 0;
}


As I learn more, I shall post more and more in here, just as Sub has done with his thread. By the end of the class, I shall be able to code a program, and code in XNA as well. I may try to make my own Indie game on Xbox.
 
Member
✔️ HL Verified
🌟 Senior Member
Joined
Oct 16, 2006
Messages
379
Best answers
0
Location
the Netherlands
When dealing with input from a user, you always need to make sure it's valid. Scanf returns the number of items assigned to variables so you can check if it's assigned correctly.

http://beej.us/guide/bgc/output/html/multipage/scanf.html said:
Return Value
scanf() returns the number of items assigned into variables. Since assignment into variables stops when given invalid input for a certain format specifier, this can tell you if you've input all your data correctly.
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
Hmm I shall learn more on that.
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
You all ready know this, but it's badass that you're doing this. I never learned C, so I can only be of so much help, but I'm sure you'll get the hang of it in no time if you stick with it.
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
Well, the teacher at the college literally said that he can't make me a programmer, he can only coach me. I understand that completely. I have been teaching myself stuff and you guys have helped me a bit with stuff as well.
Once I get the hang of everything, I think I will start off by messing with small games and programs.
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
I started to learn a lot faster once I started messing around by taking on projects. Two games that can be made in a console and are relatively easy to make are Tic Tac Toe and a clone of Drug Wars. I managed to make Drug Wars when I barely knew what functions were.
 
Force Pit Member
Joined
Dec 1, 2002
Messages
874
Best answers
0
Location
Gothenburg, Sweden
That is awesome DS! I'm rooting for you! Whenever somebody from the forum tries to learn something productive with computers, I'm literally filled with envy but at the same time very proud of my "peers"! Awesome bro, and stick to it!
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
I'm so hyped about learning it. I think I may try to learn the Drug Wars thing like you did.
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
This was mine

If you do end up trying your hand at it, you just need to know how to use functions, loops, and if statements (or a case statement, but I don't even know if that exists in C), what the modulus operator is (I used it to calculate how much drugs you could buy with your money on hand), and you need a simple random number generator.

For random number generation, you should be able to find a simple function for this on google, it doesn't need to be fancy for a game like this.
 
Last edited:
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
Alright. I think I may use this for a project or something. I could actually probably make this into Extra Credit. My teacher loves when we code on our own time.
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
Code:
{
	//declare variables and types
	float x, y, z;
	int n;
	
	//location for goto
MENU:
	system("CLS");
	//menu options
	printf("\nThe Math Program");
	printf("\nEnter Number of Desired Action:");
	printf("\n1 = Do the Power Son");
	printf("\n2 = Do some Sinning Son (Enter Degrees)");
	printf("\n3 = Exit Program\n__> ");
	scanf("%d",&n);

	//decision to continue or terminate program
	//if (n<1 && n >= 2) exit(0);
	if (n != 1 && n != 2 ) exit(0);

	//input values for variables
	printf("\nInput a value for x __ ");
	scanf("%f", &x);
	if ( n == 1 )
	{
	printf("\nInput a value for y __ ");
	scanf("%f", &y);
	}

	//process values
	if (n == 1) z = pow(x,y);
	if (n == 2) z = sin(x*3.14159/180.0);

	//output result of processing
	printf("\nThe value of z is %0.4f\n\n", z);
	system("PAUSE");

	//send control to identified instruction
	goto MENU;

	//finish up	
	return 0;
}

My first menu! Awwww shit son!
 
Last edited:
NOT IN THE MANGA™
★ Black Lounger ★
✔️ HL Verified
🚂 Steam Linked
💻 Oldtimer
Joined
Jan 5, 2008
Messages
3,276
Best answers
0
Location
Lithuania
Did a teacher/lecturer teach you goto? Unless it's just for example purposes, I don't see why would anyone encourage the usage of it.
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
Yeah, goto is evil. Especially if you're using goto to go to a point that's before the goto statement.
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
Yea, the teach pretty much said use goto for now till we get in deeper.
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
Main:
Code:
#include "stdafx.h"

//This will convert between Fahrenheit and Centrigrade/Celsius  
//
//the following file contains necessary libs and prototpes
#include "Arith3_L.h"
//
int main(int argc, char *argv[])
{
//declare variables
float tempin,tempout;
int option;
//
MENU:
//
system("CLS"); //call dos command to keep menu at top and clear
//
printf("Choose One of the Options Below:\n");
printf("\t1.Input Fahrenheit and Get Centigrade\n");
printf("\t2.Input Centigrade and Get Fahrenheit\n");
printf("\t3.Quit the Program\n");
printf("\tEnter Corresponding Number Here >> ");
scanf_s("%d",&option);

//
//do the appropriate conversion
switch (option)
{
case 1: //selected Fahrenheit to Centigrade conversion
        printf("\nEnter the Fahrenheit temperature >> ");
        scanf_s("%f",&tempin); 
        tempout = FahrtoCent(tempin);
        printf("\nThe Temperature in Centigrade is %5.1f\n\n",tempout);
        system("PAUSE");
        goto MENU;
        break;
case 2: //selected Centigrade to Fahrenheit conversion
        printf("\nEnter the Centigrade temperature >> ");
        scanf_s("%f",&tempin);  
        tempout = CenttoFahr(tempin);
        printf("\nThe Temperature in Fahrenheit is %5.1f\n\n",tempout);
        system("PAUSE");
        goto MENU;
        break;
case 3: //selected Quit option
        printf("\nTerminating! Goodbye!\n\n");
        system("PAUSE");
        break;
default: //not following directions!
         printf("\nYou Did Not Enter Valid Number!\n");
         printf("\nTry Again!\n\n");
         system("PAUSE");
         goto MENU;
         break;
}
//
  return 0;
}
Source File:
Code:
//Arith3_F.cpp


#include "stdafx.h"

//here is the program to convert from C to F
float CenttoFahr(float cent)
{
      float a1,fahr;  //these variables visible only inside
      a1 = (9.0f/5.0f) * cent; // * means multiply
      //note the use of 9.0 instead of just 9 to stress float
      fahr = a1 + 32.0f;
      return fahr;  //this is the output of the function
}
//here is the program to convert from F to C
float FahrtoCent(float fahr)
{
      float a1,cent;
      a1 = (fahr - 32.0f);
      cent = a1 * (5.0f/9.0f);
      return cent;
}
Header:
Code:
//Arith3_L.h
//
#include "stdafx.h"
//
//Declare the functions used from temperature.c 
float CenttoFahr(float cent);
//
float FahrtoCent(float fahr);
 
Last edited:
Member
✔️ HL Verified
🌟 Senior Member
Joined
Oct 16, 2006
Messages
379
Best answers
0
Location
the Netherlands
Achievement unlocked: Your first function! (+20xp)
Achievement unlocked: Naming conventions? Say what? (-10xp)


Yay functions!

Like I said before, check if the users input is valid. One golden rule(imo) is that users are usually retarded or make mistakes.

Another thing that's wise to do is to stay with one naming convention(FooBar() vs foo_bar()).
Usually in C the common naming convention is to use lowercase and seperate words with an underscore. eg:
Code:
void fire_missile(vector3_t *dir)
{
	int my_awesome_int = 0;
	rocket_t *rocket;
	
	rocket = create_rocket();
	vector_add(rocket->velocity, dir);
}
I'm not saying that you should use a specific naming convention, as long as you stick with one and one only.
Also, in the main code block, the indentation looks wrong(maybe copy-paste error?)

Anyway, keep on the good work! Don't forget to enjoy it ;)
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
I am slowly learning this. Thanks for the help. It's pretty tough to learn this quickly but I am enjoying it. After this class I have the option to learn C# or C++ and more than likely I am going for C++.
 
Member
✔️ HL Verified
🌟 Senior Member
Joined
Oct 16, 2006
Messages
379
Best answers
0
Location
the Netherlands
Both C# and C++ are cool languages, but if you really want to get experienced in memory management I would go with C++ as it literately forces you manage your memory, just like C.
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
That sounds like the right idea to me. C++ is something I had wanted to learn for a great while anyway.
 
New Member
Joined
Nov 24, 2001
Messages
692
Best answers
0
If you opt for C++, make sure the course covers C++11, rather than the oldschool stuff. C# (or Java, Scala) are much cleaner languages to learn Object Oriented programming in and most programmers (including me) are more productive in these languages. I tend to avoid having to do anything in C++ whenever I can, but as a programmer you probably will have to work with C++ code every now and then, so you can't escape learning some of it at some point. I would suggest learning both C++ and one of C#, Java or Scala. If you learn C++ first, you'll have a hard time, but learning the others afterward will be a breeze; if you learn one of the others first it will be a more gradual learning experience.
 

Users who are viewing this thread

Top Bottom