C++ :: How To Implement Symbolic Constants To Factor Out If Else Statements

Feb 17, 2014

My assignment is to create a simple stock broker program that ask the user how much they are willing to invest and ask what company they would like to invest in. Finally it outputs how many shares the user will have based on their investment amount. My code is below. My professor said to declare symbolic constants and factor out the if else statements. Ive been struggling trying to understand constant variables. How do I use const variables to factor out the if else statements?

#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
//Declare Variables
const double BAC = 16.7;
const double Citigroup = 49.52;

[code]....

View 4 Replies


ADVERTISEMENT

C/C++ :: Difference Between Literal And Symbolic Constants?

Jun 29, 2014

"A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants never change in value. You must initialize a constant when it is created. C++ has two types of constants: literal and symbolic.

A literal constant is a value typed directly into your program wherever it is needed. For example, consider the following statement:

long width = 5

This statement assigns the integer variable width the value 5. The 5 in the statement is a literal constant. You can't assign a value to 5, and its value can't be changed.

The values true and false, which are stored in bool variables, also are literal constants.

A symbolic constant is a constant represented by a name, just like a variable. The const keyword precedes the type, name, and initialization. Here's a statement that sets the point reward for killing a zombie:

const int KILL_BONUS = 5000;

Whenever a zombie is dispatched, the player's score is increased by the reward:

playerScore = playerScore + KILL_BONUS;

If you decide later to increase the reward to 10,000 points, you can change the constant KILL_BONUS and it will be reflected throughout the program. If you were to use the literal constant 5000 instead, it would be more difficult to find all the places it is used and change the value. This reduces the potential for error."

what's the difference? Here is a program to demonstrate what I'm having trouble conceptualizing.

#include <iostream>
using namespace std;
int main() {
int width = 10, length = 10;
int area = width * length;
cout << "Width: " << width << endl;
cout << "Length: " << length << endl;
cout << "Area: " << area << endl;
return 0;
}

Now, why would it be harder to go in and changed a regularly defined integer than one defined with the 'const' keyword proceeding it? For example, the width and length variables. My confusion comes from the point that they seem to both simply be variables with a value assigned to them. I feel as if the process of having to change a literal constant's value is synonymous to the process of having to change a symbolic constant's.

View 3 Replies View Related

C++ :: Calculate Surface Area Of Various Shapes - Symbolic Constant For The Value Of PI

Mar 2, 2013

This error keeps coming.

******************************************************************
CSCI 240 Program 5 Part 2 Spring 2013

Programmer:
Section: 1
Date Due: 3/1/13

Purpose: This program uses functions to calculate the surface area of various shapes. It is an exercise in learning to write functions.
******************************************************************/

#include <iostream>
#include <iomanip>
using namespace std;
#define PI 3.14
//Symbolic constant for the value of PI

int Menu();
int getValue( string prompt, int lowerBound, int upperBound );

[Code] .....

View 10 Replies View Related

C/C++ :: Factor Game Using Loop

Feb 12, 2015

So I'm doing this problem where the user will input a number that will be factored until the number is 1, 2 people will play this game and they will take turns entering a number that the initial number(named gameN) will be divided by until it is 1. Whoever get it to 1 wins. My problem is I'm not really sure how to do this, I'm pretty new to programming so besides some if statements and a loop, the problem shouldn't require too much to write. Some rules for the game are, the number entered to factor the gameN cant be less than 2, and has to divide evenly into gameN, which I have signifies with modulus. Here is where I'm at so far.

#include <stdio.h>
int main(){
int gameN;
int p1f;
int p2f;
printf("What number should the game be played with?");
scanf("%d", &gameN);

[Code] ....

View 2 Replies View Related

C :: Efficiently Multiplies A Number By A Factor 2

May 1, 2013

Write a C-program that efficiently multiplies a number by a factor 2 to the power n. The number to multiply and n are variables, which get a value at the start of the program.

Clue: 1 shift to the left is the same as multiplying by 2. 2 shifts to the left are the same as multiplying by 4. 3 shifts to the left are the same as multiplying by 8

View 7 Replies View Related

C/C++ :: Greatest Common Factor Of Many Numbers

Jun 15, 2014

How can I calculate GCF of many numbers? I thought I could calculate two by two numbers, but it not seems to be a very effective idea. There is my function:

int gcf (unsigned int x, unsigned int y)
{
return (y == 0) ? x : gcf (y, x % y);
}

View 3 Replies View Related

C++ :: Factor Out Data With Different Values To Match To Another Value

Feb 4, 2014

How will you factor out data with different values to match to another value.

For example:

value A=100
value B=50,10,20,5,15,20,30

How will you construct a logic that the values on value B will have an equal value as value A.

100 = 50+20+10+20 or 100 = 50+30+20 and so on and so forth.

I am stuck.

View 2 Replies View Related

C :: How To Find Biggest Prime Factor Of A Number

Feb 10, 2015

i want to write a program which find the biggest prime factor of a number for example the biggest prime factor of six is three or the biggest prime factor of fifteen is five. What is my program bug

Code:

#include <stdio.h> // main functions
#include <math.h> // for sqrt function
int main()
{
int i, j, k, f; // F = Flag;
printf("Enter K

[Code]...

View 9 Replies View Related

C++ :: Finding Largest Prime Factor Of A Number?

Mar 10, 2013

I am trying to find the largest prime factor of a number. But when I am trying to determine if a number is a prime number in the function:

int is_prime(int number), I am unable to exit the loop.

Here is the code:

#include<iostream>
using namespace std;
int is_prime(int number) //the problem is in this function {
int num = number;
int factor=0;
do{
num++;
for(int i=1;i<(num+1);i++){

[code].....

So when the program runs, it first divides 20 by 2, to get 10, then divides 10 by 2 to get 5. Since, // condition 1 is not met, it passes 2 to the function int is_prime(int number). The function is able to return 3, but cannot exit the loop when num is 4.

I think the problem lies in the function: int is_prime(int number).

View 5 Replies View Related

C++ :: Sending Constants As Parameters

Nov 21, 2013

What's the problem with the following:

Code:
#define K 3;
int max(int a, int b) {
return a>b? a : b;
} int main() {
cout<<max(K, K+3);
return 0;
}

Why is it not allowed, and how is it different from:

Code:
int max(int a, int b) {
return a>b? a : b;
} int main() {
cout<<max(3, 3+3);
return 0;
}

View 3 Replies View Related

C++ :: Getting Vowels And Constants From String

Nov 18, 2013

I am working on a code that is suppose to get vowels and consnants from a string. So far i got up to trying to get the vowels from a string. this is what i have so far:

#include <iostream>
#include <string> // includes go into header
using namespace std;
int getword(string word);
int getvowels(string word);

[Code] .....

View 2 Replies View Related

C/C++ :: Setting Constants As Attribute?

Apr 3, 2015

I would like to know how can i set a constant attribute in the constructor. This attribute is an int value that cannot be changed.

For instance:

class Test {
public:
const int x;
public:
Test(const int val);

[code].....

With this code i get compile error!

View 5 Replies View Related

C++ :: Program To Find Smallest Real Factor Of A Number

Oct 10, 2013

I need codes for a program in C or C++ that will show the real factor (the smallest one, without the number 1) when an integer is given as input.

When the program is started, it will ask to enter a number and press enter. After entering a number, it will first check if it is a prime number or not. If prime, it will notice that it is a prime number, otherwise, it will print the smallest real factor of the given integer.

For example, if 12 is entered, it will print, the smallest real factor for this number is: 2

If 27 is entered, it will print, the smallest real factor for this number is: 3

...and so on

View 12 Replies View Related

C++ :: How To Template Float / Double Constants

Nov 19, 2014

I have a templated class that can either use float or double as type.

My question is now: What do I do with constant numbers in the code?

Let's assume I have a multiplication by 0.5:
In the case of float type, I want: 0.5f
In the case of double type, I want: 0.5

One answer would be to check for every constant the type and doing an if/else, but this is very annoying with lots of constants.

The code using these constants infer their type automatically by the assignment, that's why I have to take care of the constants.

View 1 Replies View Related

C :: How To Make The Preprocessor Compare Against Constants Defined

Mar 6, 2015

was just curios to know if there is a way to make the preprocessor compare against constants defined like this: Code: uint uint_max = -1;

View 1 Replies View Related

C++ :: Displaying Constants As Decimal / Hexadecimal / Letter

Oct 13, 2014

I need to write a program in which you do the following:

Define three named constants using the appropriate data types:
DEC_NUM = 65;
HEX_NUM = 0x7a;
LETTER = 'f';

Then display each of these constants in decimal, in hexadecimal, and as a character using cout. Your program will have a total of nine cout statements.

View 1 Replies View Related

C++ :: Strcmp On Linked List Data And String Constants?

Sep 28, 2013

why strcmp() doesn't return true when comparing a string constant with a string that was acquired via a linked list. By the way, the data in the linked list was taken from a text file. Does that imply that there's a new line () character in the string from the linked list?

Code:
struct Node{
char ACNO[15];
struct Node *next;

[Code]....

View 1 Replies View Related

C :: Why Constants And Variables Stored In Memory In Opposite Order Called

May 23, 2014

I'm playing around and wrote a tiny program.

Code:

char bracketin[] = "thisgetsbracketed.txt";
char bracketout[] = "bracketed.txt";
char testwalk[10] = "12345678";

[Code]....

I'm incrementing the pointer to buffer 150 bytes beyond its reserved 50. I see testwalk, followed by bracketout, followed by bracketin printed by the overflow on buffer.

The memory locations are ordered descending from their call order. Why is this the case?

One would think that they would be written in ascending order as I call them. I can only assume that they're compiled bottom up - where could I read about this process?

View 3 Replies View Related

C/C++ :: If Else Statements Being Ignored

Feb 7, 2015

My problem is that some of my conditions are not being triggered when the price level of a medium or large discount is smaller than the cost of the widget(defined as .40) An example being ,base price, $2 med discount 80%, and large discount 90%. My med discount is,1.6(80%) off of 2, so $.40, which is fine, but 90% off mean 1.8(90%) off of a base of $2, which is $.20 which is too low., and should trigger the 4th if statement, Nick you are discounting large purchases too much!. However when I run this in the program, I am triggering the first if statement.

#include <math.h>
#include <stdio.h>
#define widget_cost 0.40
int main (){
float sellprice;
float discount_med_price;

[Code] ....

View 2 Replies View Related

C :: Boolean Value Use For If And Else Statements

Apr 20, 2013

Am i using boolean values correctly? our professor wanted boolean value use for the if and else statements.

Code:
#include <stdio.h>
#include <math.h>
#define GRAVITY 9.8
#define LIMIT 500
#define SEC 5

[Code] ....

View 2 Replies View Related

C :: IF Statements Within Loops

Jan 29, 2014

I've just started learning the C language, and I'm stuck on something that is probably quite simple.how to get IF statements working within WHILE loops. My code is below. I am trying to write a program to count the number of words in a sentence, and obviously stop counting after a full stop has been entered. I created a variable called 'spaces' which will increase by one after the user enters a space. However, when the IF statement is in place, the loop never terminates, even if I enter a full stop. When I delete the IF statement, the loop functions correctly.

Code:

#include <stdio.h>
int main()
{
char x;
char a;
char y;
int spaces = 0;
}

[code]....

View 4 Replies View Related

C++ :: How To Use If Statements With Strings

Jul 3, 2013

I've got a very simple but annoying problem.

if (letter3=="
"){
letter3==letter;
PrintCharacterLineEnd();

This is a code. the string "letter3" contains the string "
". What I want to happen is when, letter3 contains the text "
" I want to go to the function PrintCharacterLineEnd.

However, as of right now it's not working.

View 14 Replies View Related

C/C++ :: Using Strings And If Statements?

May 3, 2012

The problem with this code is that there are no errors showing, but when i compile the program, the if else statements do not carry out as they should do. Both if statements show the same answer, for example the second if statement. If I type Y or N then I get the same thing, 'You will now choose an event'. How do i get rid of this problem? Is char supposed to be used or string?

#include <iostream>
#include <iomanip>
#include<stdlib.h>

[Code].....

View 1 Replies View Related

C :: For Loop Skipping Statements

Jan 26, 2013

im a newbie C user and im having a little trouble in these for loop of mine im using. the first iteration is all fine but on the second and succesive iterations the first gets statement is skipped. im making a program that would ask the user to input multiple informations for atleast 5 people. i was also asked to use structures.. here is the code i have come up so far.. ive been stuck in it for like 3 hours now.

Code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
}

[code]....

gets part for the line "Enter ID Number is skipped on the second iteration..

View 3 Replies View Related

C :: If Statements For Dice Rolling

Oct 9, 2014

I'm very new to C Programming and am doing some homework for my intro to C class. "There is a game where one can play and bet money on. Two dice are rolled and the numbers that appear on the dice are added together. If the total is 7 or 11, then the user wins. If the total is 2 or 12, then the user loses. If a different total appears. then the user gets their money back."

I have written the program below:

Code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void main()
{
int d1,d2,sum;
d1=0;
d2=0;
}

[code]...

However, after finishing the program, I realized that I NEEDED to use random numbers!!! How do I make my program generate random numbers within a range of 1-12? Also, how do I make my program generate NEW random numbers EVERY TIME instead of ONCE??

View 1 Replies View Related

C :: Statements Within Braces Are Indented

Apr 25, 2013

When we say that the statements within the braces are indented, we mean a tab character distance? For example if we have :

Code:

// K&R Style
void some_function(void)
{
<1 tab>int x=1;
<1 tab>printf("Inside the function");
while(x<3) {
<1tab><1tab>printf("Inside the loop");
x++;
}
return;
}

That is right?

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved