New Posts  All Forums:Forum Nav:

New to C, have a Q

post #1 of 6
Thread Starter 
Hi, I'm learning electronics engineering at school since the start of Jan and programming is my weakest course. We just learned IF AND OR stuff in class so I used that knowledge to make a resistance calculator for parallel circuits (mathematically the formula is 1/RT = 1/R1+1/R2+1/R3, where RT is total resistance and the calculations only allow for 3 resistor values). Here is the code:
Code:
#include <stdio.h>
#include <math.h>

main()

{
        float R1;
        float R2;
        float R3;
        float sum, recip;

    printf("Parallel Circuit Resistance Calculator");
        printf("\n======================================");
        printf("\n\nPRESS Enter TO CONTINUE");
        getchar();
                printf("\nR1: ");
        scanf("%f",&R1);
                if(R1==0)
        {
                printf("Cannot have a value of 0!");
        }
                else
        {
                printf("R2: ");
        scanf("%f",&R2);
        }
                if(R2==0)
        {
                printf("Cannot have a value of 0!");
        }
                else
        {
                printf("R3: ");
        scanf("%f",&R3);
        }
                if(R3==0)
        {
                printf("Cannot have a value of 0!");
        }
                else
        
        getchar();
        sum=((1/R1)+(1/R2)+(1/R3));
        recip=pow(sum,-1);
        printf("\n\nThe resulting total resistance is: %.3f", recip);
        getchar();
                printf("\n\nThanks for using this program, press any key to exit!");
        
getch();

}


I'm happy with it working thus far, what I'd like to know is how I could maybe polish the app and streamline the code. One thing that bothers me is at the first "PRESS Enter TO CONTINUE" prompt, you can hit any key outside of enter and input will be printed after the text until Enter is hit. How can it be written so that no other input is accepted? Also, how can it be modified to allow for infinite input values, in case there are less than 3 or more than 3 resistor values to add?

Help would be appreciated, thanks smile.gif
Black & Green
(12 items)
 
Dev Box
(7 items)
 
 
CPUMotherboardRAMHard Drive
Core2 Duo E7400 Asus P5Q Hyper-X  Sandisk 
OSPower
Fedora 22 Thermaltake 650W 
  hide details  
Reply
Black & Green
(12 items)
 
Dev Box
(7 items)
 
 
CPUMotherboardRAMHard Drive
Core2 Duo E7400 Asus P5Q Hyper-X  Sandisk 
OSPower
Fedora 22 Thermaltake 650W 
  hide details  
Reply
post #2 of 6
Well, for one-line 'if' statements, you do not need the braces:
Code:
if (R3 == 0)
     printf(...);

... is perfectly acceptable and saves you one or two lines, depending where you put the braces usually. Otherwise it looks fine.
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #3 of 6
Here's a really good article on the "Press a key to exit/continue" subject. http://www.cplusplus.com/forum/articles/7312/
Check it out.

As for cleaning up your code, you could hand the setting of your resistances off to a function:
Code:
bool setResistance(float &RT)
{
        if(RT == 0)
        {
                printf("Cannot have a value of 0!");
                return false;
        }
        else
        {
                return true;
        }
}

And then instead of just continuing on after invalid input, you could put your input into a loop:
Code:
do
{
        printf("\nR1: ");
        scanf("%f", &R1);
} while(!setResistance(R1));

You also might want to look into finding a way to check if your input is actually a number. I think there's a few different ways of doing this, depending on what compiler you use. Unfortunately standard C++ does not offer a good solution for this (that I know of).

Here's my code all put together:
Code:
#include 
#include 

bool setResistance(float &RT)
{
        if(RT == 0)
        {
                printf("Cannot have a value of 0!");
                return false;
        }
        else
        {
                return true;
        }
}

main()
{
        float R1, R2, R3;
        float sum, recip;

        printf("Parallel Circuit Resistance Calculator");
        printf("\n======================================");
        printf("\n\nPRESS Enter TO CONTINUE");        
        getchar();
        
        do
        {
                printf("\nR1: ");
                scanf("%f", &R1);
        } while(!setResistance(R1));
        
        do
        {
                printf("\nR2: ");
                scanf("%f", &R2);
        } while(!setResistance(R2));
        
        do
        {
                printf("\nR3: ");
                scanf("%f", &R3);
        } while(!setResistance(R3));
        
        getchar();
        sum=((1/R1)+(1/R2)+(1/R3));
        recip=pow(sum,-1);
        
        printf("\n\nThe resulting total resistance is: %.3f", recip);
        getchar();
        printf("\n\nThanks for using this program, press any key to exit!\n");

        getch();
}

Hope I could help smile.gif
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
post #4 of 6
Thread Starter 
Thanks for the help! This bool stuff is a bit beyond my current level of skill/education. I need to hammer on the basics a bit more; still making simple mistakes with stuff. This program was just something I did independent of the class work, I hope to be making more complex programs soon smile.gif
Black & Green
(12 items)
 
Dev Box
(7 items)
 
 
CPUMotherboardRAMHard Drive
Core2 Duo E7400 Asus P5Q Hyper-X  Sandisk 
OSPower
Fedora 22 Thermaltake 650W 
  hide details  
Reply
Black & Green
(12 items)
 
Dev Box
(7 items)
 
 
CPUMotherboardRAMHard Drive
Core2 Duo E7400 Asus P5Q Hyper-X  Sandisk 
OSPower
Fedora 22 Thermaltake 650W 
  hide details  
Reply
post #5 of 6
Quote:
Originally Posted by Petrol View Post

Thanks for the help! This bool stuff is a bit beyond my current level of skill/education. I need to hammer on the basics a bit more; still making simple mistakes with stuff. This program was just something I did independent of the class work, I hope to be making more complex programs soon smile.gif

Cool man. The bool type is really just a fancy way of representing 0 (false) or 1 (true). The setResistance() function could have just as easily returned int values instead of bools and still worked.
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
post #6 of 6
Quote:
Originally Posted by Petrol View Post

Thanks for the help! This bool stuff is a bit beyond my current level of skill/education. I need to hammer on the basics a bit more; still making simple mistakes with stuff. This program was just something I did independent of the class work, I hope to be making more complex programs soon smile.gif

Here's a quick crash course on bools. They're really simple.

To declare a bool:
Code:
bool aBoolean;

They can be initialised to 'true' or 'false'. This is all a bool denotes. 1 or 0.
Code:
// Initialise boolean to true
bool aBoolean = true;

// Also initialise boolean to true
bool anotherBoolean = 1;

// ...and false
bool aFalseBoolean = false;

You can use them however you want, when you require something to happen depending if the bool is true or false:
Code:
if (aBoolean == true) {
// do something
}

You can also do:
Code:
if (aBoolean) {
// do something if true
}

Doing it like above is the same as saying "if aBoolean is true". Similarly, an exclamation mark denoting not this can be used to test whether it is false:
Code:
if (!aBoolean) {
// do something if false
}

Hopefully this clears some things up for you! Bool's really the simplest type to use.
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming