
25th May, 2007, 09:15 AM
|
| Member | | Join Date: January 2005 Location: Brighton
Posts: 4,851
| |
| Basic Stuff Just something I'm working on at Uni. Fairly basic stuff.
Copy and paste in to Notepad and save as Filename.c (Not .txt.).
Find the file in command prompt and compile it then run it.
(Type gcc filename.c to compile, then type filename.exe to run)
Tell me what ya think so far. I'm obviously new to all this stuff so it is only a command prompt program. No GUI or anything. Quote:
#include <stdio.h>
#include <string.h>
#include <math.h>
void option1();
void option2();
void option3();
char inputLine[100]; // Used to get inputs from the user
int main() { // Main routine
// Header Scren Variables
int option;
// Header Screen
while(option != 5)
{
printf("Please select which option you would like to execute, by pressing the corresponding number.\n\nOption 1---: Calculate the roots of quadratic equation ax^2+bx+c. Supply the values of a,b and c.\nOption 2---: Enter how many names you have. The program will print out different combinations of the full name given.\nOption 3---: Supply two integers which will then be used to calculate a*x^n.\nOption 4---: Supply a positive integer. Program will decide of integer is a power of 2. \n\n");
fgets(inputLine,sizeof(inputLine),stdin);
sscanf(inputLine,"%d",&option);
if(option==1) {
option1();
}
else if(option==2) {
option2();
}
else if(option==3) {
option3();
}
else if(option==4) {
printf("\n");
}
else if(option==5) {
printf("\n");
}
else{
printf("\n\nYou have entered an invalid option. Please try again.\n\n");
}
}
}
void option1() {
double vara;
double varb;
double varc;
printf("Please enter values for a, b and c.\n");
fgets(inputLine,sizeof(inputLine),stdin);
sscanf(inputLine, "%lf", &vara);
fgets(inputLine,sizeof(inputLine),stdin);
sscanf(inputLine, "%lf", &varb);
fgets(inputLine,sizeof(inputLine),stdin);
sscanf(inputLine, "%lf", &varc);
double result;
result = ((0 - varb) + sqrt(varb*varb - 4*vara*varc)) / 2*vara;
printf("\n%The answer is: \n",result);
}
void option2() {
char inputLine[100]; // Used to get inputs from the user
// Variables to store results in
int var1;
char firstName[100];
char surname[100];
char middlename[10][100];
printf("How many names do you have?");
fgets(inputLine,sizeof(inputLine),stdin);
sscanf(inputLine,"%d",&var1);
printf("Please enter your first name: ");
fgets(inputLine,sizeof(inputLine),stdin); // Get the input string from the user
sscanf(inputLine,"%s %s",&firstName); // Parse the input string and store the result
int i;
for(i = 0; i < (var1 - 2); i++) //loop for entering middle names
{
printf("Please enter middle name %d", i+1);
fgets(inputLine,sizeof(inputLine),stdin); // Get the input string from the user
sscanf(inputLine,"%s",&middlename[i]);
}
printf("Please enter your surname: ");
fgets(inputLine,sizeof(inputLine),stdin); // Get the input string from the user
sscanf(inputLine,"%s %s",&surname); // Parse the input string and store the result
// Output formatted sentences (firstName[0] is used to obtain the person's first initial)
printf("\nHello %s", firstName);
for(i = 0; i < (var1 - 2); i++)
{
printf(" %s", middlename[i]); //loop the result
}
printf(" %s\n\n\n", surname);
}
void option3() {
double a;
double x;
int n;
printf("Please enter values for a and x\n");
fgets(inputLine,sizeof(inputLine),stdin);
sscanf(inputLine, "%lf", &a);
fgets(inputLine,sizeof(inputLine),stdin);
sscanf(inputLine, "%lf", &x);
double result;
} |
__________________ Quote: |
Originally Posted by Wolf2000me | Quote: |
Originally Posted by dsio I was searching for a synonym for testicles that rhymed with hats | E6400 - 2GB Geil Dragon Quad-Channel - X1950Pro Exclusive Haddaway - What Is Love? Remix |