Anyone program C++ ?

i switched it to 100 for the final project.

The name is a max of 14 characters anyways
 
anyone got an idea why this WHILE loop wont stop when 'q' or 'Q' is entered at etype? It prints out "You have choosen to end this program. The results will be printed below." but it just keeps looping. For some reason the WHILE isnt reconizing the Q char in the etype and terminating the loop.

#include

#include

char etype;

float uSum, uSal, cSum, cSal, hSum, hSal;

float averageU, averageC, averageH;

int uCount, cCount, hCount;

main ()

{

uSum = 0;

cSum = 0;

hSum = 0;

cout
 
while (etype != 'q' || etype != 'Q')

should be

while (etype != 'q' && etype != 'Q')

(true || false) == true, so it keeps looping
 
or picture it like this

bool done = false

while(!done)

{

...

else if(eType == 'q' || eType == 'Q')

{

cout
 
back at it...this time I cant fiqure out why it says my void statement infront of my functions is an error:

#include // Bank Account Balance Program

#include

char choice = 'P';

float account = '0', deposit, Cvalue;

void float Cheque (float &x)

{

cout > Cvalue;

x -= Cvalue;

}

void float Deposit (float &x)

{

cout > deposit;

x += deposit;

}

void float Print (float x)

{

cout
 
You can only have one return type specified for a function. void float blah(... makes no sense. you want void, unless you're returning a variable with a return statement.
 
I have no return statements in my functions. The project says the functions need to be Void.
 
the function void float Print (float x) should be just 'void Print(float x)'

also, it;s good practice to declare the functions first and then implement them below main.

..

also, learn to use the switch statement to make your code more readable

 
Hey, does anyone have some spare time today to help me out (couple questions to do with using .txt files)? Instead of bumping this every 5 minutes with a question, if you had MSN that would be sweet.

PM me if you could help today :)

Thanks
 
Thanks man.

As for the switch, as in change my if/else in main to a switch with cases?

Probably a good idea.
 
so I did the switch statement but now it only terminates out of the while loop with a "Q", the small 'q' just does the case 'q' but doesnt terminate the while loop even though i have the toupper read for it

while (toupper(choice != 'Q'))

{

cout
 
You're right, it is something simple (mind you i haven't tried it...)

You probably want the while statement to be... :

while(toupper(choice) != 'Q').

aren't brackets great??? ;)

here's what I would have done in case you want reference:

bool done = false;

while (!done) // while (done != true)

{

cout
 
we havent learned about 'done' yet so I cant use it.

Thanks for all you help though!
 
it's just a bool type. bools can only be true or false, so if you give it a name like 'finished' or 'done' then this code reads like a book. You're already doing a good job by naming the keypressed as 'choice'.

 
I would just leave the else if statements. It really isn't that much more readable (to me anyway) if you you use a switch. Especially since you're having problems with upper and lowercase letters.
 
I need some help finding the right loop combinations to check if an array is sorted.

any ideas?
 
I think this works:

//checks if array is sorted from lowest to highest

bool isSorted(int array[], int array_len)

bool isSorted = true;

for(int i=0; i
 
it's either true or false, that's it.

you can assign it to conditions too - -ie: bool myBool = (1 == 1); // myBool = true
 
this is my stab and checking if an array is sorted

int count=0;

for (i=0; i < NumArr; i++)

{

Test=Array;

}

int j, temp;

for (i=0; i < NumArr - 1; ++i)

{

for (j = NumArr - 1; j > i; --j)

{

if (Array[j] < Array[j-1])

{

temp = Array[j-1];

Array[j-1] = Array[j];

Array[j] = temp;

}

}

}

for (i=0; i < NumArr; i++)

{

if (Test != Array)

{

count++;

}

}

if (count
 
so, in your example you are first saving the array, then sorting in it and checking against the saved array, right? I think I might have a better way...

bool sorted=true;

int i=0;

while(sorted==true && iarr[i+1])

sorted=false;



i++;



}

if(sorted)

cout
 
my example got messed up... lets see if this works:

int i=0;

bool sorted=true;

while(sorted==true && iarr[i+1])

sorted=false;



i++;



}

if(sorted)

cout
 
im not sure everything posted that you wanted it too.

And we use the basic functions so i cant use something we havent learned yet (bool sorted=true)
 
one more time:

int i=0;

bool sorted=true;

while(sorted==true && i"lessthan"arrLen-1)

{

if(arr"greater than"arr[i+1])

sorted=false;



i++;



}

if(sorted)

cout "sorted..";

else

cout "not";
 
i cant use bool

what i used in my orginal post is about it

mostly for loops and counts
 
my last one worked, just replace lessthan and greaterthan with the correct sign. bool is a type, it can either be true or false. So I am intially setting a variable of type bool called sorted to true, and if I find out the array is unsorted I put it to false. If you haven't learnt about bool yet you could use an int instead. So, initially create an int called sorted and set it to 1 and set it to 0 if/when you find out the array is unsorted.
 
while(sorted==0 && a"lessthan"ArrayLen-1)

what does the second part mean? the A before less than and the Len-1 ?
 
i ask because i try to run it and i get this

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(39) : error C2143: syntax error : missing ')' before 'string'

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(39) : error C2146: syntax error : missing ';' before identifier 'arrLen'

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(39) : error C2065: 'arrLen' : undeclared identifier

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(39) : error C2059: syntax error : ')'

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(40) : error C2143: syntax error : missing ';' before '{'

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(40) : warning C4552: '-' : operator has no effect; expected operator with side-effect

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(41) : error C2065: 'arr' : undeclared identifier

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(41) : error C2109: subscript requires array or pointer type

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(41) : error C2143: syntax error : missing ')' before 'string'

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(41) : error C2146: syntax error : missing ';' before identifier 'arr'

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(41) : error C2109: subscript requires array or pointer type

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(41) : error C2059: syntax error : ')'

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(42) : error C2143: syntax error : missing ';' before '{'

D:\Lecture Notes WORK\Year 2\Cosc 102 (fall)\C++ work\Assingment4part1.cpp(53) : error C2143: syntax error : missing ';' before 'string'

Error executing cl.exe.

 
just a tip... quit using global variables when you don't have to... they are no good.

and, use more brackets and spacing to make your code more readable... it is easier to find error's then,

like if (a == b && c==d) write it as

if ((a==b) && (c==d))

i just find it is easier to debug when you clearly see what is going on with your code, because when it gets more complicated... and you just throw everyhting together, it can make it very tricky to find easy mistakes.

so yeah, no more global variables, and use lots of indenting and spacing if you need to to make your code readable, and more comments... it helps in the end.
 
so this is just a chunk of code that does the actual check if the array is sorted or not, so before this part I had created an array named arr of size arrLen. The 'a' that you where referring to was a typo, sorry. So what you want is probably something like this for the sort checking algorithm Also, replace lessthan and greater than with the actual signs:

int i=0;

int sorted=1;

while(sorted==1 && i "lessthan" arrLen-1)

{

if(arr "greater than" arr[i+1])

sorted=0;

i++;

}

if(sorted==1)

cout "Array is sorted"

else

cout "Array is not sorted"
 
gah....so close

this is my function

int IsSorted(int Array[], int i)

{

int count=0, count2=0;

This is to check if the array is sorted accending

for (i=1; i < NumArr; i++)

{

if ( Array[i+1] < Array )

{

count++;

}

}

This is to check if the array is sorted decending

for (i=NumArr; i > 0; i--)

{

if ( Array[i+1] > Array )

{

if ((i+1)= 1 && count2 >= 1)

{

cout
 
ok, I don't know if you figured it out yet, but I think your problem may be that you are accessing memory outside the bounds of your array. For example if you are using 1 2 3 4

the first loop will run 4 iterations. So you compare 2 and 3, 3 and 4, 4 and ?, and ? and ?.

In the second loop you are also comparing some values to values outside your array(I marked this as ?.) See the problem is you don't know what this value is. It could be anything, so thats why I think your results are skewed. That is one of the problems people have with C/C++ is that you are able to access memory outside your array so you have. It does give you ultimate control, but you can shoot yourself in the foot.

Also arrays are indexed from 0. So the indicies you want in your example are 0-3. Try this for the 'for' part of your first array:

for(i=0; i
 
this is driving me nuts because i dont actually know what time doing with structures.

here is my program discription:

Using modular programming techniques, design and implement a C program to maintain a database

of hospital employee information. The database will store the following information about each

employee: employee number - 5 digits, name - maximum of 20 characters, age - integer, and

department (‘E’ for Emergency, ‘O’ for Orthopaedics, ‘M’ for Maternity, and ‘G’ for General

Surgery). The hospital employee database is to be implemented as an ARRAY of structures.

Use the following structure definition

struct employ_rec {

long int empnum;

char name[21];

char dept;

int age;

};

Then in your main program, declare your database similar to:

struct employ_rec hospital[50];

int numemps = 0;

The main program is to repeatedly prompt the user for input and then call the appropriate module

to deal with the request (i.e., a menu system). The legal commands in your system are:

‘A’ or ‘a’

(numemps) The routine which handles this command should prompt the user

for the all employee information. The procedure should also only input

- Add one employee to your database and add 1 to the database counteroneemployee (i.e. there should NOT be a loop in the function which allows the user

to enter more than one employee at this time). To input 2 employees requires

that the user use the

type of information input just not the correct set of values (i.e, you will get an

integer entered for employee number however it may be negative). Also be sure

to check that same employee number is not input twice.

void InsertData(struct employ_rec hospital[], int &numemps)

‘P’ or ‘p’

void PrintData(struct employ_rec hospital[], int num)

‘C’ or ‘c’

number of which employee to change, the type of information to change (‘A’ for

Age, or ‘D’ for Department’), and the new information to be inserted. Be sure

to print an error message if the employee does not exist. Also, you can assume

that the proper type of information is given but not a correct value.

void ChangeData(struct employ_rec hospital[], int num)

‘Q’ or ‘q’

A command twice. As always, you can assume the correct- Neatly print the employee information in a table (does not have to be sorted).- Change the employee information. This routine should prompt for the employee- Quit the program.

Not bad, but what i wrote has 2 major errors that i cant grasp.

here is my code (with the major error areas in bold)

*next post*
 
#include

void InsertData(struct employ_rec hospital[], int &numemps);

void PrintData(struct employ_rec hospital[], int num);

void ChangeData(struct employ_rec hospital[], int num);

struct employ_rec

{

long int empnum;

char name[21];

char dept;

int age;

};

main ()

{

struct employ_rec hospital[50];

int numemps = 0;

int choice = 'a';

cout
 
Back
Top