C++ :: Output Sorting Error Using Pointer Notation

Aug 6, 2012

My code compiled well(After long Messing up with my head). But, i still not satisfied of my output as i expected. My code ought to sort the object of person comparing their salary. But, its not.

Code:
#include <iostream>
#include <string>
using namespace std;
class person {
protected :
string name;
float salary;

[Code] ....

It doesn't sort the object of class person rather than it prints out the stored value as it is.

View 3 Replies


ADVERTISEMENT

C++ :: Linked List Node Passed As Parameter / Argument Pointer To Pointer Notation

Jan 17, 2014

I was having problems changing the value of my head node I passed it as an argument as head which would be the address. The parameter was defined as struct node *head. like this

bool deleteNode(struct node *head, struct node *delptr)

I tried manipultaing pointer values to change head node value but it did not work. I saw some code online which used pointer to pointers(in code below) to change head node value it worked I dont fully understand why. Would like better understanding of why.

Would also like to know why the argument call needed &head instead of just head.

remove = deleteNode(&head,found); opposed to remove = deleteNode(head,found);

#include "stdafx.h"
#include<iostream>
struct node{

[Code].....

View 1 Replies View Related

C++ :: Input Decimal Number And Output To Be Number Scientific Notation

Apr 26, 2013

I need to write a code in c++ , to input decimal number and the output to be number in Scientific notation with 32 bits .

View 1 Replies View Related

C++ :: Write Program To Convert Time From 24-hour Notation To 12-hour Notation

Dec 10, 2013

Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following function: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the input, and function(s) to display the results. (For 12-hour time notation, your program must display AM or PM.)

Answer:

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

[Code]....

It is showing error because may be I was not able to put that if statement inside any function. find out the error sand complete the program with corrected code.

View 2 Replies View Related

C++ :: Sorting Data - Output Names And Ages In Ascending Order

Oct 24, 2013

I am having problems sorting data... I don't know to to go about it here is my code:

<code>
#include <iostream>
#include <string>
using namespace std;
int main () {
int i;
struct person

[Code] ....

i want to sort it out so that it can output names and ages in ascending order.

View 2 Replies View Related

C++ :: Sorting Numerical Array Error

Nov 25, 2013

Why this wont run? I'm getting the cant convert int* to const char error but i do not know why?

I'm trying to create a program that creates an array of 100 ints between 0 and 250 and then sorts them using a separate function, I've gotten this far

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <cstring>

using namespace std;
void quicksort (int *items, int len);

[Code] ....

View 3 Replies View Related

C++ :: Sorting Data Based On Some Metric - Function Pointer Syntax

Nov 20, 2014

I have a piece of code that sorts data based on some metric. The some metric is something I now want to make flexible so I can easily switch between and compare metrics. To do this, I want to pass the function to use as a parameter to the method that does the sorting (and other stuff). However, I'm having problems figuring out the syntax. Also, I think my [temporary] organization of code is violating a lot of basic code design principles.

To make the function pointer passable, I defined the "typename" in the header where the function is located (it is part of a struct, "Data"):

// Below the struct definition of Data
typedef double (Data::*CostF)(unsigned l, double W) const;

The two example functions I want to use are defined in that struct ("Data"):

// Inside the struct definition
inline double someExampleCost(unsigned l, double W) const {
// Returns some basic calculation
}

The function that uses it is part of a different class (that holds a reference to the first class, in case that matters; I feel like I'm doing something odd here, because I'm binding a member function in the definition/passing, but never referencing the object). It looks like this:

// Inside another class ("Foo")
inline void DoSomeStuff(double& ECost, double& TCost, CostF cost) {
// Irrelevant stuff here
std::sort(vector.begin(), vector.end(), [&](unsigned a, unsigned b){
return (*cost)(a, W) < (*cost)(b, W);
});
// More irrelevant stuff here
}

The error shown is "operand of "*" must be a pointer". If I remove the '*':
[code]return cost(A, W) < cost(b, W);

the error becomes: "expression must have a (pointer-to-)function type."

The call to this function is, currently, just in the main function, as I'm just testing before I wrap it into real code. It looks like this:

// In main
Foo bar; // Make an object of the struct that has the "sorting" function
CostF costFunction = &Data::someExampleCost;
// Bind to the Cost function bar.DoSomeStuff(varA, varB, costFunction);

This bit shows no errors by itself. So, my questions:

a) Clearly I'm missing the insight into Function Pointers. I'm comfortable with regular pointer stuff, but I can't wrap my head around FPs, partly due to the awkward syntax.

b) I'm very uncomfortable with the fact that I'm binding a member function of a class, but never try to reference an actual object of that class. This is probably a big part of why it's not working, but I can't seem to bind a function belonging to a specific object. I thought of doing

// In the main again
Data d; // Construct the object, which contains big lookup tables
Foo F(d); // Construct the object, which only holds a reference to a Data object
CostF costFunction = &d.someExampleCost; // Bind to the Cost function of that object

but that doesn't work ("a pointer to a bound function may only be used to call the function").

View 4 Replies View Related

C :: Merge Sorting The Linked Lists - Runtime Error

Aug 10, 2013

The following program throws runtime error.

Code:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;

[Code] .....

View 3 Replies View Related

C++ :: Reverse The Output From A Pointer?

Feb 27, 2012

Write a C program to read the list from the file and store them in the arrays. Your program should write the list of client's account number, client's name and the client's balance to another file called "newdata.txt" in reversed order and also display them on the screen.

An example of output dialog is shown below

Account Name Balance
800 Stacy 100.10
700 Michael 81.05
600 Dale 1005.30
500 Richard 214.89
400 Stone -45.23
300 White 0.00
200 John 345.67
100 Jones 24.50
--------

This is what I have done so far bellow here....But the only missing part is the reversed order of 'newdata.txt'contents.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
FILE *ifpt,*ofpt;

[code]....

View 1 Replies View Related

C++ :: Passing A File Pointer To A Function Is Producing Undesired Output

Oct 27, 2013

I am working on a project and decided to try something simple before I start adding items. I am calling a function from main and that function has a file pointer.

Here is my main.cpp

Code: #include <cstdio>
#include <string>
#include <iostream>
#include "main.h"
extern FILE *fp;
using namespace std;
int main(int argc, char *argv[])

[code]....

Here is the function:

Code:
#include <string>
#include <cstdio>
#include <iostream>
#include "main.h"

[Code] ....

My test file consists of several characters and digits. Nothing special and I at this point in time do not have any type of formatting that needs to be adhered to. I am simply wanting to read the file character by character and print it out. When I run the program, I get this symbol:

Code: If I use a printf statement, such as:
Code: printf("%s
", nextChar);

I get a segmentation fault.

My main.h Code:

#ifndef MAIN_H
#define MAIN_H
void scanner(FILE *);
//char getChar(FILE *);
#endif and lastly my scanner.h Code: #ifndef SCANNER_H
#define SCANNER_H
FILE *fp;
#endif

View 6 Replies View Related

C++ :: Switch Statement - Error At Output

Jul 26, 2012

I have a question related to switch statement, which the switch in class employee2 gives me error at output. Program compiled well. I am using Code::Block Compiler V10.5. I have created object of class employee2 in main() function to get data from user, store it and display it. At the output, Compiler doesn't show actual output what i am expecting. The fun thing is my compiler printed emocion like (), where (hourly/monthly/weekly) was supposed to print.

Code:
#include <iostream>
using namespace std;
enum period {hourly,weekly,monthly};
class employee2 {
private :
char ch;
period x;
double compensation;

[Code] ....

View 10 Replies View Related

C++ :: Logical Error In Output Of DAYNAME

Oct 31, 2014

I have to write a program that does the following: Monkey Business 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 two-dimensional 3 - 7 array , where each row represents a different monkey and each column represents a different day of the week. The monkeys are represented by integers 1, 2, and 3; the weekdays are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday". The program should first prompt the user to input the data for each monkey, starting with "Sunday" for monkey #1, then monkeys #2 and #3, followed by "Monday" for monkey #1, then monkeys #2 and #3 and so on, through "Saturday". The program then creates a report that includes the following information, each properly labeled (see below):

-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. When a negative value is entered, the program outputs "invalid (negative) food quantity -- re-enter" and attempts to reread the value . Prompts And Output Labels: Decimal values should be displayed using default precision, i.e. do not specify precision. Each item read should be prompted for by a string of the form "Enter the food eaten by monkey #N on DAY:" when N is 1 or 2 or 3 and DAY is "Sunday" or "Monday" or ... or "Saturday".

The output should be of the form:

Average food consumed daily: 6.23
The least daily food consumed was by Monkey #0 on Friday
The most daily food consumed was by Monkey #2 on Sunday where the specific amount of food or specific monkeys or specific days identified depend on the actual input.

However after the process of resolving the errors unable to convert to standard string and unresolved externals I now have a logical error that causes the output of the DAYNAME variable to output as what appears to be hex in the following picture:

How I can fix this and why it's casting them as this in the output.

My full code is as follows because I don't know exactly, which line of code is causing this to happen:

Code:
//
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// constants
const int MONKEYS = 3;
const int DAYS = 7;

[Code] .....

View 5 Replies View Related

C++ :: Hash Table Program - Sorting Pointer Table After All Of Values Entered

Nov 19, 2013

I am having an issue with my sort function. This is one part of the Hash table program. The main issue is that I am trying to sort the pointer table after all of the values have been entered. The ptr_sort function is not a class function and so I am therefore unable to use the class variables psize and pTable. Is there another way I should be trying this? it is a Vector should I use the sort() function from the vector class in STL?

#include "/home/onyuksel/courses/340/progs/13f/p9/util9.h"
#include "/home/onyuksel/courses/340/progs/13f/p9/hTable.h"

#ifndef H_TABLE1
#define H_TABLE1
void ptr_sort ( );

[Code] ....

View 1 Replies View Related

C++ :: Plus Or Minus Notation

Aug 4, 2013

Is there a simple notation to check if a value is within a plus or minus range?

E.g.
//I read a value A. delay(50); //Read value again -calling this value B delay(50);
//Read value again -calling this value C delay(50); //Read value again -calling this value D delay(50);
//Read value again -calling this value E
Check IF first value A is within 5 of the value B and within 5 of value C, etc.

I can think of a few round about ways of doing this but is there any simple "equals to plus or minus" notation? (what I actually want to do is to check a lot more values than this and it will get very complicated with any of my solutions)....

View 4 Replies View Related

C++ :: Incorrect Vector Output Loop Error

Nov 21, 2014

My loop is outputting data incorrectly. I have inbound web data that come in sets of 7. I am trying to in insert the 7 records into a vector and then display the vector content followed by a new line.

Code:
char buff[BUFSIZ];
FILE *fp = stdout;
char * cstr = buff;
vector<std::string> vrecords;
while(std::fgets(buff, sizeof buff, fp) != NULL){
for(int i = 0; i < 6; ++i){

[Code] ....

expected output:

Found buy!
198397652
2014-11-14 15:10:10
Buy
0.00517290
0.00100000
0.00100000
0.00000517

[Code] ....

View 14 Replies View Related

C++ :: Scientific Notation To Decimal?

Mar 12, 2014

I just wrote code that is a program for a relativity calculator. However many of my outputs (because the values tend to be large) end up in scientific notation. Although useful, its not great for the laymen, or nice looking.

How can I change it so that output is not in scientific notation? here is the code:

// This program/converter is designed to find the desired 'real' values using Einstein's theory of relativity

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

[Code]....

View 1 Replies View Related

C/C++ :: Calculate Postfix Notation

Feb 15, 2015

My program suppose calculate postfix expression.

Ex. 5 4 + 3 10 * + the answer is 39 because if I change it to infix, it's (5 + 4) + (3 * 10)

I need to use vector to compute the value. Here is what I think. First, I save leftmost from the string. If it is a number, I push. If that is a operation, I pop twice and push the result. By doing it until the string is emptied, the vector will only contain the final answer. And here is my code

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
vector<int> stack;
string input;

[Code] .....

When I put 1 1 + or 2 2 +, it showing me a correct answer, but when I put the above example which is 5 4 + 3 10 * +, it shows 30 instead of 39.

View 8 Replies View Related

C/C++ :: Infix To Postfix Notation

Sep 14, 2014

I'm trying to make an infix to postfix notation calculator. The difficult thing is th stack class is custom. It's not to hard to understand, I don't know if the fact that it is that way will not allow me to receive support. The difference is that the pop functions is as such:

stack<char> conversion;
char temp;
conversion.pop(temp);//It receives a parameter and puts the popped element in there.
conversion.peek(temp);//Places the top element in said parameter

not only that... but these are boolean functions. they return true if the operation was completed. So they can be used as conditions.

#include<iostream>
#include<string>
#include<stdio.h>
#include"stack.h"
using namespace std;
int main(void) {
Stack<char> conversion;
string infix,inter,temps;

[Code] .....

The error is that i am mismanaging parenthesis handling and i can't seem to grasp where and how.

An example input is:(35+7)-(9-2)
that input gives me:35 7 + 9 2 ) -
but another input such as :(35+7)/7
outputs as: 35 7 + 7 /. Totally fine.

View 3 Replies View Related

C++ :: Stop Scientific Notation

Jul 15, 2013

double number = 10000000;
int range;//the length of the string result
string result;//holds the number in a string
ostringstream convert; //stream used for the conversion
convert << number;
result = convert.str();

range = result.length();

I'm trying to convert a double to a string and when the number goes to ten million it goes to scientific notation and it shows it in the string. How do I stop it from do that?

View 2 Replies View Related

C++ :: Array / Pointer Giving Error?

Jul 20, 2013

The program should ask the user how many students were surveyed and dynamically allocate an array of that size. The program should then allow the user to enter the number of movies each student has seen.

#include <iostream>
#include <string>
using namespace std;

[Code].....

The problem I'm having is that where I declare movies = [numStudents]; the semicolon after the numStudents array is giving me this error - "error: expected a '{' introducing a lambda body".

View 2 Replies View Related

C/C++ :: Error - Cannot Convert To A Pointer Type

Apr 3, 2015

I have attached the source code I am writing. I have been getting the two following errors. I know it's something to do with the pointer casting.

udp-server.c:174:3: error: subscripted value is neither array nor pointer nor vector
udp-server.c:165:3: error: cannot convert to a pointer type

The file is attached here.
udp-server-edit.txt (7.64K)
Number of downloads: 19

View 3 Replies View Related

C/C++ :: Pointer To A Function Used In Arithmetic Error

Nov 23, 2014

I'm working on a short program to calculate the mode of a vector of ints. I am new, so not extremely familiar with pointers, and passing items to functions. This is something I've struggled with (obviously, or I wouldn't be here). I am currently getting the following error when I try to compile this program using g++:

warning: pointer to a function used in arithmetic

I receive this error for the following lines: 66, 73, 75, 81.

I am not using pointers here so I do not understand why this error crops up, much less how to fix it. Here is the code I am struggling with:

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <limits>
using namespace std;
vector<int> getModes(vector<int> userValues);

[Code] ....

The errors are on lines 54, 61, 63, and 69

View 3 Replies View Related

C :: Program To Print Letters In Given Notation

Feb 11, 2014

I have to develop script in C to print the alphabets in given notation.

Please check the attachment.

View 4 Replies View Related

C/C++ :: Reverse Polish Notation Not Doing Second Operand

Sep 24, 2014

been working on this code til my eyes bled, can not get second operand to work, if i enter 1 2 3 + - it will only return 5 from the addition operand.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

[Code].....

View 5 Replies View Related

C/C++ :: Program For Adding Many Fractions With Sum Notation

Jul 16, 2014

I was trying 2 write a program that would calculate the sum notation of 1/(i^2) with the starting number to be 1 and goes up to the nth term. For instance if the user inputed 3 then the sum would look like 1+1/4+1/9. I somehow made a code but it gets weird numbers which some include negative numbers... when I input a number that is above 5.

#include <stdio.h>
#include <math.h>
int main(int argc, const char * argv[]) {
int n;
register int i=1;
float b;//For part 1

[Code] ....

For some reason I can't edit printf("%f",/>/>; when I post it as the topic so ignore that part cuz Ik its supposed to be written as printf("%f",/>;

View 6 Replies View Related

C/C++ :: Find Out Scientific Notation Of Numbers

Oct 21, 2014

how i solve this..?

View 1 Replies View Related







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