C/C++ :: Function Not Returning Values - Structs In Arrays

Feb 25, 2015

I wrote this code for a homework assignment, everything runs fine but the function void percent_votes (line 66) isn't calculating properly, it outputs 0.00 for each value. I have tried everything I can think of to try and make it work.

Here is the assignment: Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate's name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election.

Here is the code I have written:

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

[Code].....

View 4 Replies


ADVERTISEMENT

C++ :: Outputting Data From File Into Structs And Arrays Of Structs

Apr 15, 2013

I am having a lot of trouble being able to get data from a file and input it into given structs and arrays of structs and then outputting the file. We are given a file that contains 96 lines and looks like this:

Arzin, Neil
2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6
Babbage, Charles
2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5

This file continues for 24 different people and then repeats with different scores (the second line).
The first number, in this case is 2.3 for both people is a difficulty rating. The next 6 numbers are scores.

We are given this data in order to set up our structs and arrays and my code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main () {
ifstream inFile;
inFile.open("C://diveData.txt);

[Code] .....

View 1 Replies View Related

C++ :: Write A Function Returning 2 Values?

Jul 23, 2013

So I have a function like the one below

bool get_array( float input_param, vector<int>& output_param ){
// some code that handles output_param output
if( error ) return false;
}

So basiclly this function must return 2 value well one of the value is just a bool returning whether the procedure doesn't fail at some point or not.

is there any better way to write this function ?

View 7 Replies View Related

C :: Array Function Not Returning Correct Values

Apr 1, 2013

I have been trying to make a function that compares all the values of the array and if they are all equal will return a value to print true. The problem I am having is that regardless of what values I enter the function is always returning true. Any way to tell the program o check all the values in one command instead I put them each,

Code:
#include<stdio.h>
#include<stdlib.h>

int compare(int arrayA[], int arrayB[]);

[Code]....

View 2 Replies View Related

C++ :: Pass 2 Arrays Into Void Function And Return Values To One Function?

Feb 12, 2014

I'm trying to pass 2 arrays into a void funtion, and return values to one function.

this is the the program I'm working with, after I'm done I have to split it into 3 files, a header, a main, and a separate cpp file for the functions to live in.

#include <iostream>
using namespace std;
void processArrary(int numberCount[], int Numbers[], int intnumberSize, int numberCountSize);
int main() {
int Scores[26] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189};
int numberCount[8] = { 0 };

[code]...

The goal of this program is to separate and count the groups of numbers then output the amount of numbers in each group. Near as I can tell, everthing should work, but I'm getting all zeros to be displayed in each group.

View 6 Replies View Related

C++ :: Structs And Arrays

Jul 30, 2014

If i want to input (FROM A FUNCTION) information into a struct:

* How do I define/ call/ prototype that function.
* How do I check if that array inside the function is empty?

View 1 Replies View Related

C :: Structs With Double Arrays

Apr 19, 2014

The main is not working properly, I get strange missing parenthesis notices and array errors which I am not understanding. How to identify the errors. Functions are included.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int menu ();
double change_value (double * value);

[Code] ......

View 3 Replies View Related

C++ :: Using Structs And Arrays For Grading?

Jul 3, 2012

Write a program for the following problem. You're given a file that contains a collection if IDs and scores (type int) for an exam in your computer course. You're to compute the average of these scores and assign grades to each student according to the following rule:

If a student's score is within 10 points (above or below) of the average, assign a grade of satisfactory. If a studnt's score is more than 10 points above average, assign a grade of outstanding. If a student's score is more than 10 points below average, assign a grade of unsatisfactory.

The output from your program should consist of a labeled three-column list that shows each ID, score, and corresponding grade. Use a struct to store each student's data and an array of structs to store the whole class. The struct should have a data member for id, score, and grade.

I was given the following file grades:

3313904258647075100
568888487970797094
4700504489737073100
956188698887846398
31999669100908867100
376878578059571560
82917256708274983
7754766293100784158
81469468999493954
2106984796947027100

So far, I have this much, but the output is all wrong. I also haven't tackled the grade part yet.

Code:

//grader.cpp
//Grades

#include <fstream>//required for file streams
#include <iostream>//input/output
#include <cstdlib>//for definition of EXIT_FAILURE
#include <string>
#include <iomanip>
using namespace std;
#define inFileGrades "grades.txt"//grades

[code]....

View 4 Replies View Related

C :: Setting Values For Array Of Structs

Dec 18, 2013

How do I set all of these string and double value(s) to 0?

Code:

#define MAXcharacters 12
#define MAXaccounts 100
struct Records {
char userid[MAXcharacters + 1];
char password[MAXcharacters + 1];
double balance;
};

[code]...

View 3 Replies View Related

C :: Function That Merge Content Of Two Sorted Arrays Of Double Type Values

Nov 4, 2013

I need to write a function that will merge the content of two sorted arrays of type double values. The function should not assume that both its input parameter arrays are the same length.

Here is my program so far - I know there is alot of errors and mistakes:

Code:

#include<stdio.h>
void merge(int firstArray[],int secondArray[],int size) {
int mergedArray[size],i=0,j=0,k=0;
while(i<size||j<size) {
if(i==size) {

[Code]...

View 2 Replies View Related

C :: Returning Values From While Loop

Jul 13, 2013

I am currently doing a bodyfat calculator for a course in school.I'm having a problem getting an output returned from my while loop, which determines the user's gender by user input.My function (outside of main) is currently:

Code:

char gendercheck(gender){
while (true) {
gender = _getch();
if (gender == 'M' || gender == 'm') {
system("cls");
printf("Your selected gender is male.");
break ;

[code]....

My failed tries include "return gender" and "return genderpicked" statements in both the if-functions and the loop itself, which I have deleted since that didn't work. My goal is to get a character stored in gender, so that I can use the information of the user's gender in my main function from this point and onwards.

MY MAIN IS:

Code:

int main() {
char *genderpicked ;
char gender ;
genderpicked = &gender ;

[code]....

View 9 Replies View Related

C++ :: Returning Values To Main

Nov 5, 2014

I am working on this program below. The problem I am having is trying to return totalDays to the main. In the numOfDays() function there is a for loop that adds the totalDays (totalDays = totalDays + days). I have just been getting errors in returning the correct amount of totalDays to the main from the numOfDays function.

#include <iostream>
using namespace std;
int numOfEmployees();
int numOfDays(int);
int main() {
int totalDays = 0;

[Code] ....

View 2 Replies View Related

C++ :: Returning Multiple Values?

Oct 9, 2013

I would like to return multiple values from one function to access in different functions. For example:

int function1(//what goes here?)
{
int a ;
a = 1 + 2 ;
int b ;
b = 3 + 4 ;

return (what goes here if i want to return the value of a and/or b to use in another function?) ;

void function2()
{
//now i want to use the value of a here to use in an equation, how do i do that?
//now i want to use the value of b here to use in an equation, how do i do that?
}

View 4 Replies View Related

C++ :: Functions Returning Values?

Apr 9, 2014

I have an assignment in my C++ class that is to create a menu based coffee shop program. Here is what I have so far:

#include<iostream>
#include<cmath> //doubt this is needed but i added it just in case
using namespace std;

[Code]....

View 19 Replies View Related

C++ :: Passing / Returning Character And Integer Arrays With Functions

Jun 21, 2013

Passing and returning character and integer arrays with functions using simple programs...use pointers if necessary

View 1 Replies View Related

C :: Assigning Values To Arrays / Printing Arrays

Jul 1, 2014

Using a for loop, construct two 100 element arrays, x and y, such that element i of x stores the value sin(2*pi*i/100)) and the corresponding element of y stores cos((2*pi*i/100)). Print the values stored in the elements of x and y as you calculate them.

I have attempted to solve it but I'm not sure why the value 0 is only being printed, maybe I haven't assigned sin(2i/100)) and cos((2i/100)) to the arrays properly?

Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main () {

[Code] .....

View 3 Replies View Related

C/C++ :: Switch Case Statement Not Returning Correct Values

Mar 21, 2015

My code is supposed to read a five to four digit code of a resistor from a file and determine the resistor's nominal, lower and upper tolerance values. I have inputted my file's first resistor code as a string so I could run tests on it but the problem that occurs is that a get the wrong values from my switch-case statements. I have put printfs after the function call to see what the values were and they turned out wrong. If I could get my return values to be right then I could be on my way coding.

#include<stdlib.h>
#include<stdio.h>
double bandNum(char x); // function prototype that will read the resistor value for the first, second and possibly third band
double bandMult(char x); // function prototype that multiplies the resistor by some value of 10
double bandTol(char x); // function prototype that the tolerance of the resistor is multiplied by

[Code] ....

View 5 Replies View Related

C++ :: Initializing Loop And Returning Values Rock Paper Scissors

Dec 2, 2013

When i return 0; at the function int userselect (int user, int comp)my output displays twice and when i ask the user to run it again the computers selection of rock, paper or scissors remains the same it doesn't find a new random number. So i made the function userselect return to main(). This works and my output is displayed once, but when i run it it skips my while part of my do while loop completely so it loops the program without asking the user if they wish to run it again.

but when i return main() the computers choice re-randomizes every time so i need to find a way to get the while part of my do while to initialize and the program to not loop without the option of ending it.

#include <iostream>
#include <string>
#include <cmath>

[Code]....

View 1 Replies View Related

C++ :: Returning Values Or Using Pointers To Redefine Variables Passed As Arguments?

Feb 28, 2014

Which is more efficient in functions? Returning values or using pointers to redefine variables passed as arguments?

I mean either using:

void ptr_Func(int *x)
{
*x = *x+1
}

or

int ptr_Func(int x)
{
return x + 1;
}

In terms of speed, memory use etc.I want to know general efficiency, I know it will obviously vary with different uses and circumstances.

View 7 Replies View Related

Visual C++ :: GetMost And GetLeast Functions Returning Erroneous Values

Apr 5, 2014

After compiling, I cannot understand why my getMost and getLeast functions are returning erroneous values. I've done everything I could think of to try and fix this, Problem and code below.

A local zoo wants to keep track of how many pounds of food each of its three monkeys eats each day during a typical week. Write a program that stores this information in a twodimensional 3 * 7 array, where each row represents a different monkey and each column represents a different day of the week. The program should rst have the user input the data for each monkey. Then it should create a report that includes the following information:

* Average amount of food eaten per day by the whole family of monkeys.
* The least amount of food eaten during the week by any one monkey.
* The greatest amount of food eaten during the week by any one monkey.

Input Validation: Do not accept negative numbers for pounds of food eaten.

Code:
#include <iostream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

// Constants.
const int NUM_MONKEYS = 3;
const int NUM_DAYS = 7;

[Code] ....

View 3 Replies View Related

C++ :: Using Structs To Define A Function

Sep 27, 2013

What I'm trying to do :

struct foo {int x; int y;};
foo function_example(int x, int y) {
foo temp;
temp.x = x;
temp.y = y;
return temp;
}

Works as is, however when I try doing it through seperate class files :

//header file for class example_class
struct foo {int x; int y;};
foo function_example(int x, int y);

//source file for example_class
foo example_class::function_example(int x, int y) {
foo temp;
temp.x = x;
temp.y = y;
return temp;
}

I get an error telling me that foo is undefined, and that declaration of function_example(int x, int y) is incompatible with the declaration of it in the header file.

View 1 Replies View Related

C++ :: Function Returning A Value

Feb 20, 2015

I am making a game commonly know as the Hangman using C++.

Now I am trying to add a man in it like this:

0
|/
|
/

Now the problem i am facing is that i am using a check that if a function returns the value 0 "return 0" it means the guess is wrong and it will not update the man but if it returns any value there will be a function called which will update the man.

I just wanna know that how i am going to use the check, the kind of thing that i am trying to use is, in general words "if(function returns a value) then update the man"

int main() {
return match;
}

How are we going to use it in check that if int main is returning 'match' in the check...

View 3 Replies View Related

C++ :: Int Function Not Returning A Value

Jan 22, 2014

This simple little program is not returning a value. The output is

" (string) contains characters" (The number of characters is supposed to display between the 'contains' and 'characters.'

However, if I go to the function and cout the length, the cout in the main body displays just fine.

Here's the main portion :

cout << "'" << input << "' contains "; //Output of character count.
charCount(input);
cout << " characters, including any spaces.
"; //Output of character count.

and here's the function.

int charCount(char *string) {
int length = 0; //Variable to hold the number of characters.
//Gets the number of characters contained in *string and puts that number into length.

[Code] .....

View 2 Replies View Related

C :: Returning Pointer From Function

Nov 21, 2014

As the title says, i'm using a function which returns a pointer to a struct:

the struct is the following:

Code:
typedef struct POINT
{
uint16_t x;
uint16_t y;
}

Coordinate; the function i'm using:

Code:
Coordinate * Read_XTP2046(void)
{static Coordinate screen;
//calculations to determine the coordinates
screen.x=(temp[1]+temp[2])/2;
screen.y=(temp[0]+temp[2])/2;
// and so on...
return &screen;}

The question is: how do i catch this pointer and make it into a Coordinate struct in which i can read the x and y.

In my main program i would do the following:

Code:
Coordinate cor;
cor = Read_XTP2046();

This does not work, as the function returns a pointer, but how to transform this pointer into a Coordinate struct.

View 8 Replies View Related

C :: Returning Back To Another Function

Mar 7, 2014

Here's a small portion of my program:

Code:
int choice(void) {
char buffer[BUFSIZ];
char answer;
printf("

[Code] .....

I am wondering which is correct to use

Code: return choice();
or
Code: choice();
return num;

View 4 Replies View Related

C :: Returning Function To Array?

Mar 31, 2013

I need to create a function which will print a list from 100Hz to 1000Hz then 1000Hz to 9000Hz. I have created a function in order to calculate and set up the frequency values from 100Hz to 9000Hz using two for loops as shown below. However I am unsure how to return this to the array at the main.

int main(void) {
double Frequency[18];
system ("PAUSE");
return(0); } double Frequency (void)
{
int count;

[Code]....

View 1 Replies View Related







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