David W 131 Practically a Posting Shark

You also might like to see that using classes some times just adds code bloat.

Take a look (no classes) ...

// test_invalid_time_simple.cpp //


#include <iostream>
#include <iomanip> // re. setw, setfill
#include <string>
#include <cctype> // re. toupper


using namespace std;

void print24HourTime(int hr, int min, int sec, const string& );


// used here to take in valid numbers //

int takeInInt( const string& msg, int min, int max,
               const string& errmsg = "\nIntegers only please ...\n\n" )
{
    int val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
        {
            if( val >= min && val <= max ) break;
            //else ... if reach here ...
            cout << "Valid input range here is: " << min << ".." << max << "\n";
        }
        else
        {
            cout << errmsg;
            cin.clear(); // clear error flasgs
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}

int takeInChr( const char* msg )
{
    cout << msg << flush;
    int c = cin.get();
    cin.sync(); // 'flush' cin stream
    return c;
}
bool more()
{
    int c = takeInChr( "More (y/n) ? " );
    if( c == 'n' || c == 'N' ) return false;
    // else ...
    return true;
}



int main ()
{
    do
    {
        string str;
        for( ; ; )
        {
            // do this part firstly ...
            // so can see that following hr is AM/PM //
            cout << "Enter AM or PM: ";
            cin >> str;
            cin.sync(); // …
David W 131 Practically a Posting Shark

You might like to see this edited logic for the 24 hour part ???

Note: if you added min and max values to the int takeIn function ...
then you would NOT need all the code in your exception classes
as all the error messages could be handled by the min.max bounds there
and the relevant errmsg passed.

// test_invalid_time.cpp //


#include <iostream>
#include <iomanip> // re. setw, setfill
#include <string>
#include <cctype> // re. toupper

#include "invalidHr.h"
#include "invalidMin.h"
#include "invalidSec.h"

using namespace std;

int getHours();
int getMinutes();
int getSeconds();
void print24HourTime(int hr, int min, int sec, const string& );



// used here to take in valid numbers //
template < typename T >
T takeIn( const string& msg, const string& errmsg = "\nValid numbers only please ...\n\n" )
{
    T val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;
        else
        {
            cout << errmsg;
            cin.clear(); // clear error flasgs
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}

int takeInChr( const char* msg )
{
    cout << msg << flush;
    int c = cin.get();
    cin.sync(); // 'flush' cin stream
    return c;
}
bool more()
{
    int c = takeInChr( "More (y/n) ? " );
    if( c == 'n' || c == 'N' ) return false;
    // else ...
    return true;
}



int main ()
{
    do
    {
        string str;
        for( ; ; )
        {
            // do this part firstly ...
            // …
David W 131 Practically a Posting Shark

This example of taking in valid numeric input,
in an loop,
into a STL vector container,
and then finding sum, average, min, max,

using C++ library functions ...

this is a fairly common C++ beginning student type problem.

It may give you some ideas about how to get started to recode,
especially your first problem above ...
and how to use a modular design, (how to functions).

// takeInPositiveFindAverage.cpp //

#include <iostream>
#include <iomanip> // re. setprecision, fixed
#include <vector>
#include <numeric> // re. accumulate( begin, end, startVal )
#include <algorithm> // re. min_element, max_element


using namespace std;


// used here to take in valid positive integers
template < typename T >
T takeIn( const char* msg, const char* errmsg )
{
    T val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;
        else
        {
            cout << errmsg;
            cin.clear(); // clear error flasgs
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}

int takeInChr( const char* msg )
{
    cout << msg << flush;
    int c = cin.get();
    cin.sync(); // 'flush' cin stream
    return c;
}
bool more()
{
    int c = takeInChr( "More (y/n) ? " );
    if( c == 'n' || c == 'N' ) return false;
    // else ...
    return true;
}


// used here to facilitate printing out the vector info ... //
ostream& operator << ( ostream& os, const vector< unsigned >& v )
{
    os << …
David W 131 Practically a Posting Shark

Please see the comments at the end of your other similar post.

Those comments also apply here.

Please start fresh with ONE program and indicate where you think it may have some issues ... Also describe what the program is supposed to do.

Please take some time to look at other requests for help.

That will give you a better idea how to actually get the help you would like.

David W 131 Practically a Posting Shark

Hey ... best to submit one problem at a time.

We are not machines.

I would suggest you think a lot more about, perhaps, your most pressing problem, and ask for help with that code, first.

Then explain what it is you want that code to do.

With just a quick scan of the muddled code conglomerate you just threw this way, it looks like you could use a much more modular design and thus break up big jobs into individual parts, each with its own well named function.

Also, if you wish to learn how to write portable code, then there are problems with your code in that regard.

David W 131 Practically a Posting Shark

What are you trying to do?

Providing the spec's of the problem you were asked to solve
using classes
would help us to see what you are supposed to be doing.

I'm doing a clock program

What is your clock supposed to do in your program?

Another way to phrase it that might help to get things started ...

What is the input expected (as per the spec's) ?

What output is expected for that sample input ?

David W 131 Practically a Posting Shark

Which ...

Your life ?

Your programming ?

If you do not yet know much about programming ...

And if programming really is 'your life' ...

Then ... there's still, (but maybe just a little), time to learn how to live :)

David W 131 Practically a Posting Shark

No problem ... :)

There are many ways ...

Sometimes ... simple is easier to see for students.

But yes, I prefer using a format string in many contexts.

David W 131 Practically a Posting Shark
  1    2    3    4 ....    n
  n  n-1  n-2  n-3 ....    1
============================
n+1  n+1  n+1  n+1 ...   n+1

So ... 2 * SUM = n*(n+1)
Thus       SUM = n*(n+1)/2

Can you write code to prompt and input an integer into a variable ...
(maybe label that int variable n) ?

Show what code you can do ...
then we can see where you are at
and be able to help you from there.

David W 131 Practically a Posting Shark

Take a look:

# str.istitle.py #

myStrs = [ "Sam S. Smith", "Ann a. anderson", "Anne Anderson" ]

mySet = set(myStrs) # pretend this is your big dictionary of names

for item in myStrs:
    print( "'" + item +"'.istitle() is", item.istitle() )

print()

for item in myStrs:
    print( "'" + item +"'[0] == A is", item[0] == 'A' )

print()    

for item in myStrs:
    if item[0] == 'A':
        if item in mySet:
            print( "'" + item +"'[0] == A is", item[0] == 'A' )
            print( item, "is in", mySet )
David W 131 Practically a Posting Shark

You may have to hunt the web for a big file of names ...

When you find suitable ones process and merge them into one big set (of unique names)

(Actually ...
just keep names that begin with 'A' in your set of processed names.)

Now just look up each word that beings with 'A' to see if it is in that set.

(Set look up times are very short.)

David W 131 Practically a Posting Shark

For starters, you might test each word to see if it begins with 'A' ?

If it does ...
then might look it up in a dictionary of just names to see if it is a name?

David W 131 Practically a Posting Shark

But ...

do not really need to use an array, if it's ok to process and display the even numbers as they are entered :)

@ddanbe ... we both seem to be stuck on the night shift +1 :)

David W 131 Practically a Posting Shark

But if you are using ... not excessively large,

'pre-fixed max-size receipt tapes' ...
on each of your simulated 'cash-registers' ...

probably a much better idea than messing about with dynamic memory,
would be to just use an array,
pre-fixed at compile time,
to hold each 'max-sized receipt tape'

Maybe (an edit of your code) could be ... a little something like this:

// classCashRegister3.cpp //

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

using namespace std;

////////////////////////////////////////////////////
// done here ... so can easily change and re-compile
////////////////////////////////////////////////////
const int MAX_NUM_RECEIPTS = 10;
const double TAX_RATE = 1.13;

struct Item
{
    string name;
    double price;
} ;

const Item ITEMS[] =
{
    { "Hammers", 10.99 }, { "Levels", 19.99 },
    { "Screwdrivers", 8.99 }, { "Tape Measures", 4.99 },
    { "Wrenches", 10.99  }
} ;

const int NUM_ITEMS = sizeof(ITEMS) / sizeof(Item) ;
////////////////////////////////////////////////////


// a utility used here to facilitate VALID integer input //
int takeInInt( const string msg )
{
    int val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;
        else
        {
            cout << "ERROR! Integers ONLY here please ...\n";
            cin.clear(); // clear error flasgs
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}
///////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////
class Register
{
private:

    struct Receipt
    {
        double total; // to hold dollar total for each order

        // to hold the quantity of each item ordered for an order, i.e. a receipt
        int …
David W 131 Practically a Posting Shark

And also ...

for starters ...

you need a destructor (to plug your memory leak) ...
and probably a copy constructor ...
and your code for the overloaded assignment operator is 'interesting' :)

Fix the above first ...

then (any) other errors will manifest more clearly :)

David W 131 Practically a Posting Shark

Do you understand how an 'insert-sorted' container is supposed to work, as you add/insert each next element?

Have you tested your code out with some random type data input to see if the output is correct at each step?

You might like to see some of these student type examples, to get some more ideas?

http://developers-heaven.net/forum/index.php/topic,310.0.html

http://developers-heaven.net/forum/index.php/topic,2606.0.html

http://developers-heaven.net/forum/index.php/topic,2613.0.html

David W 131 Practically a Posting Shark

If you code as per the following, you can 'see' if there was a problem finding/opening the files ...

// fileReadWrite.cpp //

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char* FILE_IN  = "input.txt";
const char* FILE_OUT = "output.txt";


int main()
{
    ifstream fin( FILE_IN );
    ofstream fout( FILE_OUT );
    if( fin && fout )
    {
        string word;
        while( fin >> word ) // read in each 'word' in the file, word by word
        {
            // do any processing here that you wish on this 'word' ... for example
            // just print it to screen and to file, each 'word' on a new line

            cout << word << endl; // write each 'word' on a new line, to the console screen
            fout << word << endl; // write each 'word' on a new line, in the output.txt file
        }

        fout.close();
        fin.close();
    }
    else
    {
        if( !fin ) cout << "There was a problem opening file " << FILE_IN  << endl;
        if( !fout ) cout << "There was a problem opening file " << FILE_OUT  << endl;
    }
}
David W 131 Practically a Posting Shark

An other way to learn is by observing the coding steps ... and code ... of master coders.

Then trying to implement what you have learned to solve similar problems.

Some prof's will, at first,
outline the code-developement/code-testing steps ... step by step.

Then you can very easily 'see' how the solution process is progressing,
as you code/test each next part,
to be added in ... in the develop/test process.

Then you can use a similar process to layout and build/test your own, top-down designed ... and built, bottom up solutions.

I would recommed a text like this one, by a seasoned teacher and master coder:

http://plantation-productions.com/Webster/www.writegreatcode.com/index.html

David W 131 Practically a Posting Shark

Post the code you have tried so far.

David W 131 Practically a Posting Shark

1stly ...

Unless you use a typedef ...
you need to use struct node (both words)

Take a look here ...

http://developers-heaven.net/forum/index.php/topic,2440.0.html

David W 131 Practically a Posting Shark

The examples at this next link might give you some ideas to get started ?

http://developers-heaven.net/forum/index.php/topic,2615.msg3134.html#msg3134

David W 131 Practically a Posting Shark

Hey Dani,

I didn't realize, until just now, that I had an uncle Jacob Javits?

Shalom shalom,

David W. Zavitz (Javits)

David W 131 Practically a Posting Shark

At some point ... you might like to see these

http://developers-heaven.net/forum/index.php/topic,2022.0.html

Six Fast Steps to Programming in C

http://developers-heaven.net/forum/index.php/topic,2608.0.html

C SOME UTILITY functions ...

David W 131 Practically a Posting Shark

You might like to see this for beginners in Python ...

http://developers-heaven.net/forum/index.php/topic,46.msg89.html#msg89

You will find that Python code is also very compact ...

A good (and it is also free online) tutorial/text is called 'Think Python'

http://en.wikibooks.org/wiki/Think_Python

David W 131 Practically a Posting Shark

You could use pointers to begin and end of array ...

/* recursiveReverseAry.c */  /* 2015-02-27 */

#include <stdio.h>


void reverseAry( int* beg, int* end );
void printAry( const int* ary, int size );


int main()
{
    int ary[] = { 1, 2, 3, 4, 5 };
    int size = sizeof(ary) / sizeof(int);
    printAry( ary, size );

    reverseAry( ary, ary+size-1 );
    printAry( ary, size );
    return 0;
}


void reverseAry(int* beg, int* end )
{
    if( beg >= end ) return;
    else
    {
        int tmp = *beg;
        *beg = *end;
        *end = tmp;
        reverseAry( ++beg, --end );
    }
}

void printAry( const int* ary, int size )
{
    int i = 0;
    for( ; i < size ; ++ i ) printf( "%d ", ary[i] );
    putchar('\n');
}
David W 131 Practically a Posting Shark

This example may help you to get started ...

// classCashRegister.cpp //  // 2015-02-27 //

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

// a utility useful here ... //
int takeInInt( const string msg )
{
    int val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;
        else
        {
            cout << "ERROR! Integers ONLY here please ...\n";
            cin.clear(); // clear error flasgs
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}



struct MyPair
{
    string name;
    double price;
    // ctor...
    MyPair( const string& name, double price ) : name(name), price(price)  { }
} ;


const MyPair ITEMS[] =
{
    MyPair("Hammers", 10.99),  MyPair("Wrenches", 10.99),
    MyPair("Levels", 19.99),  MyPair("Tape Measures", 4.99),
    MyPair("Screwdrivers", 8.99)
} ;

const int NUM_ITEMS = sizeof(ITEMS) / sizeof( MyPair );

struct Order
{
    int item[NUM_ITEMS]; // to hold quantity of each item ordered //

    // default ctor...
    Order() { for( int i = 0; i < NUM_ITEMS; ++ i ) item[i] = 0; }

    double get_total() const
    {
        double sum = 0;
        for( int i = 0; i < NUM_ITEMS; ++ i )
             sum += item[i] * ITEMS[i].price;
        return sum;
    }
    void takeIn()
    {
        for( int i = 0; i < NUM_ITEMS; ++ i )
        {
            item[i] = takeInInt("How many " + ITEMS[i].name + ": ");
        }
    }
} ;


class Register
{
    vector< Order > myOrders;
    double sum;

public:
    Register( int max_num ) { myOrders.reserve(max_num); sum = 0; }

    void get_order() …
David W 131 Practically a Posting Shark

You need to re-read the 'problem' ...

Write a c++ program that does the following:
Create a Register class that can store all of the receipts
for the day and the totals for that register.

So ... each cash-register STORES each order (could use a struct for each order)

Thus ... could use a vector of struct to hold all orders for each cash-register
also could have a member to track the running dollar sum of orders so far

David W 131 Practically a Posting Shark

But ...

If you wished to take in ONLY 5 VALID int's,
ALL AT ONCE,
ALL on one line ...
you could code something like this:

// take in loop ...
for( ; ;  )
{
    cout << "Enter 5, space separated integers, all on one line: ";
    if( cin >> ary[0] >> ary[1] >> ary[2] >> ary[3] >> ary[4] && cin.get() == '\n' ) break;
    else
    {
        cin.clear(); // clear error flags
        cin.sync(); // 'flush' cin stream ...
        cout << "\nERROR! Only FIVE integers are valid input here ...\n\n";
    }
}

Understanding how this works:

cin >> someIntVar;

is key here!!!

cin will skip over any/all leading whitespaces until it finds a non-whitespace char

if that char is a valid int char, it will accept it and keep taking further valid int char's until a non-int char is reached, which non-int char then will be left in the cin stream

but if that first non-whitespace char was NOT a valid int char, an error flag will be set, etc...

thus ... for invalid int input, can clear error flags and 'flush' cin stream and output some error message and loop again for valid input.

David W 131 Practically a Posting Shark

You seem more than just a little lost :)

Programming can become enjoyable when you understand how to break up a problem into steps and get a working solution for each step.

The example code I gave you above ... demo's ONLY a 'first step' ...

a student way to take in valid int's, one at a time, each int being individually prompted for input.

If you wish to take in 5 int's, all on one line, all at once, that is a different problem.

You best get that part working and understood before you try to code an array class.

Note the loop if you wish to take in VALID int's, ONE AT A TIME:

// take in loop ...
for( int i = 0 ; i < 5 ; )
{
    cout << "Enter integer # " << (i+1) << " (of 5) "<< ": ";
    if( cin >> ary[i] ) ++ i; // since good input here, can ++i //
    else
    {
        cin.clear(); // clear error flags
        cin.sync(); // 'flush' cin stream ...
        cout << "\nERROR! Only integers are valid input here ...\n\n";
    }
}
David W 131 Practically a Posting Shark

You could try something like this (that also validates input) ...

int main()
{
    int ary[5] = {0}; // get 'array' to hold 5 ints ...
    int low, high;

    // take in loop ...
    for( int i = 0 ; i < 5 ;  )
    {
        cout << "Enter integer # " << (i+1)  << " (of 5) "<< ": ";
        if( cin >> ary[i] )
        {
            if( i == 0 ) low = high = ary[0];
            else
            {

                if( ary[i] < low ) low = ary[i];
                else if( ary[i] > high ) high = ary[i];
            }
            ++i ;
        }
        else
        {
            cin.clear(); // clear error flags
            cin.sync(); // 'flush' cin stream ...
            cout << "\nERROR! Only integers are valid input here ...\n\n";
        }
    }

    cout << "\nLow is  :  " << low << endl;
    cout << "\nHigh is :  " << high << endl;


    // ...


}
David W 131 Practically a Posting Shark

read the five integers the user inputs then adds it to the list which reads back to you ...

At first, just use an array of int to simulate your array class for testing and step by step development design ...

// takeIn5NumbersAndPrint.cpp //  // 2015-02-26 //

#include <iostream>

using namespace std;

const char* PAUSE = "\nPress 'Enter' to continue/exit ... ";


int main()
{
    int ary[5] = {0}; // get 'array' to hold 5 ints ...

    // take in loop ...
    for( int i = 0 ; i < 5 ;  )
    {
        cout << "Enter integer # " << (i+1)  << " (of 5) "<< ": ";
        if( cin >> ary[i] ) ++ i;
        else
        {
            cin.clear(); // clear error flags
            cin.sync(); // 'flush' cin stream ...
            cout << "\nERROR! Only integers are valid input here ...\n\n";
        }
    }

    // display loop ...
    cout << "\nshowing the 5 values entered ... \n";
    for( int i = 0 ; i < 5 ; ++ i )
         cout << ary[i] << endl;


    cout << PAUSE << flush;
    cin.sync();
    cin.get();
}

Now you can work on your array class.

David W 131 Practically a Posting Shark

Enter five scores and display the lowest score
and the difference of each score from the highest score.

You will need two loops and an array to hold the 5 scores.

In the first loop, you can take in the 5 scores into the array and also find the lowest and highest score.

Hint ... set the 1st score that you take in to be both the lowest and highest score ...

Then for the next 4 scores, compare each next score to lowest and highest and update lowest and highest as needed.

So now you can print out the lowest and highest scores ...

On second loop though the array of scores... you can find and print out the difference of each value from the 'highest value' that you found above.

David W 131 Practically a Posting Shark

Write lots of code and solve many coding problems ...

Some great coding examples can fast-track the process provided that you study the examples and DO understand them ...

You may also like to see these links for beginning CS students ...

http://216.92.238.133/Webster/www.writegreatcode.com/index.html

http://developers-heaven.net/forum/index.php/topic,46.0.html

http://developers-heaven.net/forum/index.php/topic,2607.0.html

David W 131 Practically a Posting Shark

@ddanbe: Just saying 'is' ... in a softer way :)

Friendly folks here at Dani's place ... do not want to scare off potential fan's for Dani ... eh?

David W 131 Practically a Posting Shark

or? .... could use latest Python 3 with:

import sqlite3

David W 131 Practically a Posting Shark

Or ... (sorry to be a little late getting back) ... you might like to see a type of 'table look up' solution approach .. that can reduce code bulk and streamline logic flow ...

# examplePairsInList.py #

def takeInFlt( msg ):
    ok = False
    flt = 0.0
    while not ok:
        try:
            flt = float( input( msg ) )
            ok = True
        except:
            print( "Only numbers valid input here ..." )
    return flt

def shippingRate( shipWeight, rates, topRate ):
    for pair in rates:
        if shipWeight <= pair[0]:
            return pair[1]
    else:
        return topRate


if __name__ == '__main__':

    # ratePairs is a list of pairs : [ maxLbsAndUnder, rate ] #
    ratePairs = [ [2, 1.10], [6, 2.20], [10, 3.70] ]  
    maxRate = 3.80

    more  =  True
    while more:    
        weight = takeInFlt( 'Please enter the shipping lbs: ' )
        shipRate = shippingRate(weight, ratePairs, maxRate)
        print( 'Your shipping rate: ${:0.2f}'.format(shipRate) )
        print( 'Your shipping charges: ${:0.2f}'.\
               format(weight*shipRate) )

        more = ( input( 'More (y/n) ? ' ).lower() != 'n' )
David W 131 Practically a Posting Shark

This seems to be duplicate post about the same problem as at ...

https://www.daniweb.com/software-development/cpp/threads/485784/how-to-do-standard-viariance-in-

So ... please use the above for further help ...
(i.e. ... do NOT open a 2nd thread about essentially the same problem.)

David W 131 Practically a Posting Shark

Oops ... duplicate deleted

David W 131 Practically a Posting Shark

You might like to look here to find an example of taking in numbers ... (into an array)

and finding the sum and average values ...

http://developers-heaven.net/forum/index.php/topic,2019.0.htm

You will probably want to take in a variable number of values ... so an easy student way to start to do that, is to use an array with the max size prefixed at compile time, as in the following example ... (make sure that the array size is big enough to handle ALL the expected values)

(later you could use an easily expandable container like the STL vector container)

// arrayInt.cpp

// enters integer data, finds sum, average ...

// http://developers-heaven.net/forum/index.php/topic,46.0.html

#include <iostream>

using namespace std;

int const MAX_SIZE = 4; // using a small size here for easier testing ...


int getValidInt( const char prompt[] )
{
    for( ; ; ) // an example of a C/C++ forever loop ... until 'return'
    {
        cout << prompt << flush;
        int testInt;
        cin >> testInt;
        if( cin.good() )
        {
            cin.sync(); // 'flush' cin stream as we go ...
            return testInt;
        }
        // else ...
        cin.clear(); // clear cin error flag(s) ...
        cin.sync();
        cout << "Invalid input! Integers only please ...\n" ;
    }
}

bool more()// defaults to 'true'/'yes' ... unless 'n' or 'N' entered
{
    cout << "More (y/n) ? " << flush;
    int reply = cin.get();
    cin.sync(); // 'flush' cin stream ...
    if( reply == 'n' || reply == 'N' ) return false;
    // else ...
    return …
David W 131 Practically a Posting Shark

Also ...

for examples like this:

int a = 1;
cout << "now a = " << (++(++a)) << endl; // two increments

// vs ...

int b = 1;
cout << "now b = " << (b += 2) << endl; // one addtion

// probably b is faster code
David W 131 Practically a Posting Shark

In light of what was said above by @Schol-R-LEA ...

I would like to suggest that each student compare ...

the below ...

fairly equivalent student type numeric input usage of ...

scanf

vs

sscanf

???

and see which you prefer?

(Note 1: both examples check for a '\n' char to exist right after the last digit that was input in the leading numeric digits ...

Note 2: both examples erroneously accept invalid input via overflow)

/* takeInNum_a.c */

/* scanf example with some validation of numeric input */


/*
    a simple student way to handle numeric input ...
    so program won't crash on bad input

    Six Fast Steps to Programming in C
    http://developers-heaven.net/forum/index.php/topic,2022.0.html
*/

#include <stdio.h>
#include <ctype.h> /* re. tolower */


/* 2 handy utilities for many C student coding problems ... */
int takeInChr( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' ) return 0;
    /* else ... */
    return 1;
}


/* a simple student way to handle numeric input ...
   so program won't crash on bad input */
int takeInInt( const char* msg )
{
    int done = 0, val = 0;
    while( !done )
    {
        printf( msg ); fflush( stdout …
David W 131 Practically a Posting Shark

Just saying ...

that if I was using a MinGW compiler set to C90 or C99 ... I observed these messages:

C:\MinGW\examples\test_long_double\checkOutDouble.c In function 'takeInDbl':
28 9 C:\MinGW\examples\test_long_double\checkOutDouble.c [Warning] unknown conversion type character 'L' in format [-Wformat=]
28 9 C:\MinGW\examples\test_long_double\checkOutDouble.c [Warning] too many arguments for format [-Wformat-extra-args]
C:\MinGW\examples\test_long_double\checkOutDouble.c In function 'main':
48 9 C:\MinGW\examples\test_long_double\checkOutDouble.c [Warning] unknown conversion type character 'L' in format [-Wformat=]
48 9 C:\MinGW\examples\test_long_double\checkOutDouble.c [Warning] too many arguments for format [-Wformat-extra-args]

And ... an example run: (Note erroneous output ...)

Enter a decimal: 123
You entered 0.000000
More (y/n) ?
Enter a decimal: 4567890
You entered 0.000000
More (y/n) ?
Enter a decimal: 98765.4321
You entered 0.000000
More (y/n) ?

See also:

http://en.wikipedia.org/wiki/Long_double

... With the GNU C Compiler, long double is 80-bit extended precision on x86 processors regardless of the physical storage used for the type ...

http://www.mingw.org/

Enter a decimal: 123
You entered 0.000000
More (y/n) ?
Enter a decimal: 456789
You entered 0.000000
More (y/n) ?

David W 131 Practically a Posting Shark

You might like to also try data entry something like this:

# takeInMatrix.py #


def takeInRow( numCols ):
    data = input( "Enter "+str(numCols)+" numbers separated by a space: ")
    items = data.split()

    good = True
    cols = []
    for item in items:
        try:
            num = float(item)
            cols.append( num )
        except:
            print( "There was some invalid data ... " )
            good = False
    return good, cols




def takeInMatrix( rows, cols ):
    matrix = []
    i = 0
    while i < rows:
        good, row = takeInRow( cols )
        if good:
            if len(row) == cols:
                matrix.append( row )
                i += 1
            else:
                print( "\nMust be", cols, "numbers entered ..\n" )
        else:
            print( "Try again ... " )

    return matrix



mat = takeInMatrix( 3, 5 )

for row in mat:
    print( row )
David W 131 Practically a Posting Shark

I think that long double is not supported in many (most?) C compilers ...

So use someting like this:

#include <stdio.h>
#include <ctype.h>


/* 2 handy utilities for many C student coding problems ... */
int takeInChr( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' ) return 0;
    /* else ... */
    return 1;
}


double takeInDbl( const char* msg )
{
    double dbl;
    for( ; ; )
    {
        printf( msg );
        if( scanf( "%lf", &dbl) == 1
            && getchar() == '\n' )
            break;

        /* else ... if reach here ... */

        printf( "\nInvalid entry ... "
                "only numbers allowd here ... \n\n" );
        while( getchar() != '\n' );
    }
    return dbl;
}



int main()
{
    do
    {
        double decimal = takeInDbl( "Enter a decimal: " );
        printf( "You entered %f\n", decimal );
    }
    while( more() ) ;

    return 0;
}
David W 131 Practically a Posting Shark

We like to help ... but if you want to learn to program then you must actually practice coding ...

Can you code an Hello World type prgram?

Can you code to ask the user to input an integer from the keboard, firstly printing out a prompt to guide the input?

Can you code for ...

if a == b then do ... else do ...

If you can do these ... you have enough coding skills to start your problem.

Show us your code for the first part ...

1.Input three unique numbers and print the different of the highest and lowest numbers

... and if you need help with that code ... we will then be able to actually see where you are at.

David W 131 Practically a Posting Shark

Did you try using type 'double'

instead of type 'long double' ?

David W 131 Practically a Posting Shark

You might like to look for an example to get some ideas to get started ...

https://www.daniweb.com/software-development/cpp/threads/484356/convert-programs

David W 131 Practically a Posting Shark

Note your line 7 could be changed to ...

int rows = 5; //, cols = 5; // cols is not used below

and you could use functions for each step ...

// allocate and initialize the array
// print the array
// free the array

David W 131 Practically a Posting Shark

Any one want to suggest using SQL and linked tables ?

(Note: Python 3 now comes with SQLite ... and that might be a nice student way to begin a program to handle these linked data 'Tables' ?)

David W 131 Practically a Posting Shark

Just an example to try to get Dani in the top listing of Google search ... of just part 'a'

(Part 'b' should be easy to add to the menu as choices 3 and 4 with function calls for each.)

// FahrenheitToCelsiusAndViceVersa.cpp //

// demo of ...
// 'how a student might do validation of numeric input'

// see "Six Fast Steps to Programming in C++"
// http://developers-heaven.net/forum/index.php/topic,2022.0.html

#include <iostream>
#include <string>
#include <cctype> // re. tolower...

using namespace std;

/*
    http://en.wikipedia.org/wiki/Fahrenheit

    Fahrenheit to Celsius : (°F - 32) ÷ 1.8 =°C

    Celsius to Fahrenheit : (°C × 1.8) + 32 =°F
*/

const string MENU =
      "1) Fahrenheit To Celsius\n"
      "2) Celsius To Fahrenheit\n"
      "Your choice (1 or 2) ? ";

// some utilities ...

double takeInDbl( const string& msg,
                  const string& errMsg =
                  "\nOnly numbers are a valid entry here!\n" )
{
    double val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;
        else
        {
            cout << errMsg;
            cin.clear();
            cin.sync();
        }
    }
    return val;
}

char takeInChr( const string& msg )
{
    cout << msg << flush;
    string reply;
    getline( cin, reply );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}

bool more()
{
    if( tolower( takeInChr( "More (y/n) ?" ) ) == 'n' )
        return false;
    // else ...
    return true;
}


// ... functions to convert ...

void FahrenheitToCelsius()
{
    double f = takeInDbl( "\nEnter Fahrenheit temperature to …