Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 Help please, a few questions, C programming

views
     
TSRViN
post Feb 15 2006, 09:24 PM, updated 20y ago

There Is Only One United
*******
Senior Member
5,827 posts

Joined: Jan 2003
From: Selayang, Selangor


Any help on this would be appreciated, since I am a complete newb at this sad.gif

user posted image

Thanks in advance

This post has been edited by RViN: Feb 17 2006, 08:36 PM
silverhawk
post Feb 15 2006, 09:38 PM

Eyes on Target
Group Icon
Elite
4,956 posts

Joined: Jan 2003


We will help you... but first read the following two topics

http://forum.lowyat.net/index.php?showtopic=54524
http://forum.lowyat.net/index.php?showtopic=178516

This is not a homework forum. Show some effort and we will help you figure out what you can't understand.
yingchai
post Feb 15 2006, 09:52 PM

Regular
******
Senior Member
1,281 posts

Joined: Jul 2005

hey...dude
i think u should try it out urself 1st b4 asking here

at least u should put some of ur effort in solving the question...
if u stuck half way, then post ur codings here n ask nicely...

if u post ur question like this, i m afraid the morderator might close it...
pls try it out 1st b4 posting...
we here can guide u, not spoon fed u...
TSRViN
post Feb 15 2006, 10:11 PM

There Is Only One United
*******
Senior Member
5,827 posts

Joined: Jan 2003
From: Selayang, Selangor


I was just looking at some pointers to start it off. I mean I know the basics but I dont know the equations to put in the code to make it work. Like the second one where there and nth posibilities.

hmn.. thanks anyways.
silverhawk
post Feb 16 2006, 01:31 PM

Eyes on Target
Group Icon
Elite
4,956 posts

Joined: Jan 2003


Just find out how to write equations in C. Then use your basic math knowledge to find out what equations you will need. Its pretty easy.

The nth possibility is easy too, you just have to know what the limit is. You can do this by asking for the number of pararrel lines first or counting as lines are added to the circuit.
TSRViN
post Feb 17 2006, 08:34 PM

There Is Only One United
*******
Senior Member
5,827 posts

Joined: Jan 2003
From: Selayang, Selangor


QUOTE(silverhawk @ Feb 16 2006, 01:31 PM)
Just find out how to write equations in C. Then use your basic math knowledge to find out what equations you will need. Its pretty easy.

The nth possibility is easy too, you just have to know what the limit is. You can do this by asking for the number of pararrel lines first or counting as lines are added to the circuit.
*
Thanks for the help but I manage to figure quite abit out from the notes I had though inadequate.

This is what I came up with
CODE
code removed for easier scrolling


I cant get the menu to work although the rest do work on their own though not as they are suppose to but I can sort that later. I need to get themenu to work. Any ideas?

This post has been edited by RViN: Feb 19 2006, 08:30 PM
silverhawk
post Feb 17 2006, 08:51 PM

Eyes on Target
Group Icon
Elite
4,956 posts

Joined: Jan 2003


try using
CODE

int selection = 0;


instead of char.
Also.. why are you using goto statements? ewwww
TSRViN
post Feb 18 2006, 12:28 AM

There Is Only One United
*******
Senior Member
5,827 posts

Joined: Jan 2003
From: Selayang, Selangor


QUOTE(silverhawk @ Feb 17 2006, 08:51 PM)
try using
CODE

int selection = 0;


instead of char.
Also.. why are you using goto statements? ewwww
*
Cheers for the help again. Its working, I removed the goto function.

I did a few modificationas as well. Can you please help me and make sure I done it correctly smile.gif

CODE
Code removed for easier scrolling


Thanks again. I really appreciate the time you are taking to help me out. smile.gif

This post has been edited by RViN: Feb 19 2006, 08:29 PM
silverhawk
post Feb 18 2006, 01:09 AM

Eyes on Target
Group Icon
Elite
4,956 posts

Joined: Jan 2003


I see you are adding the menu code again in each function. That is of course not a smart thing to do...

try doing this

CODE

int selection = 0;

do {
   /* menu code here */
} while ( selection != 4 );


This way, it will keep going on untill you choose option 4 which is to quit. I will leave the details for you to figure out smile.gif

TSRViN
post Feb 18 2006, 02:01 AM

There Is Only One United
*******
Senior Member
5,827 posts

Joined: Jan 2003
From: Selayang, Selangor


QUOTE(silverhawk @ Feb 18 2006, 01:09 AM)
I see you are adding the menu code again in each function. That is of course not a smart thing to do...

try doing this

CODE

int selection = 0;

do {
   /* menu code here */
} while ( selection != 4 );


This way, it will keep going on untill you choose option 4 which is to quit. I will leave the details for you to figure out smile.gif
*
Thanks again. I think thats it for now. Just need to check my formula for the calculations are correct.

biggrin.gif
TSRViN
post Feb 19 2006, 12:01 PM

There Is Only One United
*******
Senior Member
5,827 posts

Joined: Jan 2003
From: Selayang, Selangor


Ok just discovered one more problem. When the menu appers and a letter besides a number, the screen will keep on going without halting.

I think there must be a way to introduce a character function in it but how do I tie it in with the menu?
silverhawk
post Feb 19 2006, 05:20 PM

Eyes on Target
Group Icon
Elite
4,956 posts

Joined: Jan 2003


Ahh, this is where you need to do some input validation.
One simple method to do is to use a string and then convert it to int. If it fails you output an error message, if it succeeds then u can use it for the menu selection.

CODE

#include <stdio.h>
#include <string.h> /* for the string data type */
#include <stdlib.h> /* for the conversion function */

string input;
int     selection = 0;

printf("Enter an integer value : ");
scanf("%s", &input);

selection = atoi( input );

if ( input == 0 )
{
   printf("That was not an integer value");
}
else
{
   printf("Integer entered was : %d", selection);
}


That's an example, wrote it at the top of my head, so there MAY be errors. So fix it yourself smile.gif

TSRViN
post Feb 19 2006, 08:28 PM

There Is Only One United
*******
Senior Member
5,827 posts

Joined: Jan 2003
From: Selayang, Selangor


I could manage to do it that way because the string function keeps getting "unknown identifier" errors.

I did it this way:
CODE
#include<stdio.h>
void equation();
void calculate_current();
void time_conversion();
void main()
{
int selection = 0;
char input;
do
{
 printf("1.Equation Evaluation\n");
 printf("2.Current Calculation\n");
 printf("3.Time Conversion\n");
 printf("4.Quit\n\n");
 printf("Enter Selection: ");
 scanf("%d", &selection);
 scanf("%c", &input);
 printf("\n----------------------------------\n\n");

 switch(selection)
 {
 case 1:
  equation();
  break;
 case 2:
  calculate_current();
  break;
 case 3:
  time_conversion();
  break;
 case 4:
  break;
 default:
  printf("Try Again\n\n");
 }
}
while (selection !=4);
}

void equation()
{
int selection = 0;
float a, b, c, d, x1, x2, b1, b2;
printf("Enter value of A11 :");
scanf("%f", &a);
printf("Enter value of A12 :");
scanf("%f", &b);
printf("Enter value of B1  :");
scanf("%f", &b1);
printf("Enter value of A21 :");
scanf("%f", &c);
printf("Enter value of A22 :");
scanf("%f", &d);
printf("Enter value of B2  :");
scanf("%f", &b2);

printf("\nThe equations are :\n");
printf("%.fX1 + %.fX2 = %.f\n", a, b, b1);
printf("%.fX1 + %.fX2 = %.f\n\n", c, d, b2);
x1=((b1*d)-(b2*b))/((a*d)-(c*b));
x2=((a*b2)-(b1*c))/((a*d)-(c*b));
printf("X1=%.2f, X2=%.2f\n\n", x1, x2);
printf("----------------------------------\n\n");
 
}
void calculate_current()
{
float resistance, total, gtotal;

total=0;
printf("Enter value of parallel resistances, Enter 0 to end :\n");
 scanf("%f", &resistance);
 while(resistance !=0)
 {
  total +=1/resistance;
  scanf("%f", &resistance);
 }
 gtotal=1/total;

 printf("\nTotal Resistance = %.2f Ohms\n\n", gtotal);
 printf("----------------------------------\n\n");

}

void time_conversion()
{
int sec, min, hr;
printf("Please enter time in seconds :");
scanf("%d", &sec);
hr=sec/3600;
min=(sec%3600)/60;
sec=(sec%3600)%60;
printf("%d hours %d minutes %d seconds\n\n", hr, min, sec);
printf("----------------------------------\n\n");
}


Is it ok to make it like this??
I just added a new character input called input but didnt specify where its saved or used.

This post has been edited by RViN: Feb 19 2006, 08:31 PM
silverhawk
post Feb 19 2006, 09:38 PM

Eyes on Target
Group Icon
Elite
4,956 posts

Joined: Jan 2003


Did you run your code?
if you did, i think you will notice that after you enter a number, you will still have to enter a character. I don't think you want this.

Did you include the string header file (<string.h>) and the standard library header file (<stdlib.h>)? I've not touched C in a while, so try declaring the string like this instead..

CODE

char input[100]; /* String of 100 characters */



TSRViN
post Feb 19 2006, 10:18 PM

There Is Only One United
*******
Senior Member
5,827 posts

Joined: Jan 2003
From: Selayang, Selangor


The way I did it, there was no need to enter the character twice and when entered a character it will give out the default case though after doing let say function 1 and then it comes back to the menu, if I enter a character it will reperform the function rather than stating the default case.

I did include the string header file (<string.h>) and the standard library header file (<stdlib.h>) but it still agve me the error. Though after doing it with char input[100]; it worked without errors but it required me to enter the character twice to select a function and even though I entered a character or an integer it stil gave me the "You selected " messesage.
silverhawk
post Feb 19 2006, 10:50 PM

Eyes on Target
Group Icon
Elite
4,956 posts

Joined: Jan 2003


You only need to get the input once. So no need 2 scanf() functions.

Then convert the input to an integer and assign that integer to the variable selection.
TSRViN
post Feb 20 2006, 07:02 PM

There Is Only One United
*******
Senior Member
5,827 posts

Joined: Jan 2003
From: Selayang, Selangor


QUOTE(silverhawk @ Feb 19 2006, 10:50 PM)
You only need to get the input once. So no need 2 scanf() functions.

Then convert the input to an integer and assign that integer to the variable selection.
*
thanks for your input once again. I got it working from the input you gave above. I didnt do it properly the first time.

 

Change to:
| Lo-Fi Version
0.0157sec    0.14    5 queries    GZIP Disabled
Time is now: 24th December 2025 - 02:39 AM