WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

DaysFirst = How many days left in the first month
DaysLast = essentially the day of the last month - 1
DaysMonth = number of days in each month between the two months

Add them all together.

Example:
4-May-2007 to 23-Sep-2007

DaysFirst = 27 (31 days in May - 4)
DaysLast = 22 (day of the month - 1)
DaysMonth = 92 (June/30 July/31 August/31)

Total= 141 days

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

If you're having more problems, ask a question.
If this project is done, start a new one.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I have this program it allows the users to enter a name and a score it saves all the names to one array and all the scores to another. The program then displays all the names and scores in a list.

I want to have the code for displaying the the list of names and scores be in a separate function. The code works in the int main() function but as soon as i put it in a separate function the code no longer works

Why? Does it print one name only? Does it print junk? Does the program crash? Does the program cause nuclear detonation taking out Greenland?

The more vague you are, the more useless help is.

Thank you for finally using CODE tags. Now we need better titles... :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

The try/catch block is already implemented in the DoIt function but that's not the solution because the exception is caused by a third party DLL.

Talk to the third party. They have a better chance of figuring out what's going wrong.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

How can i get the info from the infile(one data stored on top of the other) to be stored into the arrays and i have? Thx for your input.

You're close.

Defining char letter[30]; gives you letter[0] thru letter[29], therefore this code

inFile>>letter[30];
	inFile>>item[30];
	inFile>>cost[30];

reads the first 3 values into the array entries after the array definitions.

You need to put the inputs into a loop, using the loop index from 0 to 29 as the index into the variables.

Also, consider using whitespace for readability. For example

//Display receipt
	cout << endl;
	cout << "Zandiago’s  Z-BURGER SHACK" << endl;
	cout << "Amount" << mealprice << endl;
	cout << "6 % Sales Tax" << tax << endl;
	cout << "============================================" << endl;
	cout << "Total" << total << endl;
	cout << "Amount tendered" << amount_tendered << endl;
	cout << "Change" << (amount_tendered-total) << endl;
	cout << "Thank you for visiting"'\n';
	cout << "Zandiago's Z-Burger Shack" << endl;
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Yes, I have, and yes I use it something went wrong with the formatting (CTRL_C CTRL_V).

Isn't it possible to fix it before you hit SUBMIT? That's what I use PREVIEW for.

Pseudocode might be bad, but it is clear what I was trying to say and it is a solution.

Bad is bad. I simply pointed out why so you could learn.

I thought that was what this forum was about last time I checked?
Moderating is one thing, insulting people another.

Yes, helping people with good code or, preferably, good pseudo code is exactly what this forum is about. Posting bad code (logically and/or formatted) is of little help to those that are trying to learn.

If you feel pointing out mistakes or bad code is an insult, sorry.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Personally, I'd read the test answers into a char* rather than a C++ string . A char* must be defined to accommodate the total number of answers necessary, but a string will read only the answers on the line. If a student doesn't answer the last 5 questions, the string could be short and cause problems. A char* will still have data, however it will have to be cleared after each use.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Try reading some of the sticky posts that are labels Read Me: to learn good posting techniques...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
_ _ _ _ function2(_ _ _ _ _ _ _ _ _ _ _ );
main{
int n[]= {4, 5, 6};
int *kptr ;
kptr = function2( n ) ;
}

Fill in the blanks and you get:

int *function2( int array[ ]); or
int *function2( int *ptr );
/* parameters names can be omitted */
main{
int n[]= {4, 5, 6};
int *kptr ;
kptr = function2( n ) ;
}

Can you please tell me why its 'int *' in the function prototype, shudnt it be just 'void'? since its not returning anything? Or shudnt it just be 'int' excluding the '*', ?

It isn't returning anything? Calling the function is accomplished by kptr = function2( n ) ; isn't it? And doesn't it load the value into kptr which is an int * ?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

When a customer orders, the cashier presses a key for each item ordered (WITHOUT PRESSING
<ENTER>). If more than one of the same item is ordered, the key is pressed an appropriate
number of times. As each key is pressed, the name of the item and its price are displayed on the
next available line on the monitor....

Ouch!!! Considering Standard CPP cannot do this, your instuctor is teaching techniques that cannot be used normally in the industry. I don't like this at all. :icon_evil:

If you look at my infile (as show before), how do i get the data from each individual line to be stored in the array?

Read the line and break it up into it's 3 parts. To learn the most, test the line character by character and process each section accordingly.

Have you considered using a struct or class for each item on the menu?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

One other thing, we hadn't eveb touched vectors and arrays yet, Duoas, had to look that up in the book, looks faster than the way we're doing it.

That's the problem with some of the help on boards like this. Many experienced programmers can't tell from someone's code that the chance of higher level language constructs are probably beyond their current course level :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

All the places where you have something like /2 check to make sure your numerator is a double before division occurs. If it isn't, change your denominator to a double (say, /2.0 ).

Or, more generally, make sure at least one of the values being divided is a floating point value.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Greetings,

I'm having a problem with this code below.

So am I. See the scanf() series here...

try using %s in the scanf's

No, better not. Here's why.

>try using %s in the scanf's
No, don't. At least don't until you know how to safely use %s and change the input variables to arrays. Otherwise you'll invoke undefined behavior and create security holes/mysterious crashes.

See above link. Although %s can be made somewhat safe, it's better to use fgets() to read strings. It's harder to create problems and control your input.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I am trying to develop a program that reads a list of integers from a file and outputs whether or not each integer in the file is perfect. Its suppose to give practice using endfile-controlled and for loops.

I'd start by attempting something. Just opening a file and not checking the error status does not even attempt to practice EOF-controlled loops. Did you forget what you read in the post titled Read Me: Read This Before Posting?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Start by reading this and this.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Read the Rules, especially this one.
Also read the sticky at the top of the forum titled Read Me: Read This Before Posting

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Double check all your { } pairs, make sure you've not left an open set in main( ). That could lead the compiler to thinking you're trying to declare a function inside a function.

The easiest way to do this is to learn how to format your code correctly.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

That's kool, but how does that help the next person that comes by and expects answers from a search? Now only those that read this thread understand heuristics, but the other 223,045 members (and uncountable lurkers) still have no clue (not that they'll read it from the search page anyway, but there's a chance). :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

For 25 posts, you sure haven't been listening to posting suggestions.

READ THE FAQ!!!! Titles are important
READ THE "READ THIS BEFORE YOU POST" STICKY!!! How you ask helps how you are helped
READ THE BBCODE STICKY!!! READ THE BACKGROUND ON THE BOX YOU TYPE YOUR MESSAGES IN!!!! How you post helps us help you

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

> How about saying that on the search page that you use a heuristic for performance reasons

I think that it can pretty much be assumed whenever querying a huge dataset that some level of heuristics are involved. There would be no positive benefit to having such a disclaimer.

No it can't. Most users that search have absolutely no idea what backend they are searching, nor the extent of the information being searched. A webmaster may understand, but non-web-gurus don't. I do not consider myself a neophyte, nor is Salem, and it seems neither of us made this obvious assumption.

I'm with Salem here.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Compilers
Code::Blocks
MSVC++ Express
Open Watcom C++/Fortran
Bloodshed DevC
Borland 5.5

Don't use Vista but they all work on XP.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

It shouldn't that much of a challenge when you're allready able to write recursion :)

- ask a user for input before you call your function
- add a 3th var to the function which is the number of times the user just entered
- in the function count down from the 3th var to zero and then quit

regards Niek

You don't need a third variable. The two you have are enough. Just think about what those two variables are for, and how they relate to the input value...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Look at your output statements. Exactly what did you output?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You could try something like this: (pseudocode)

int input, average, total, count;

while(count++) 
{
ask for input ;
if (input == 0) break; // 0 quits te program
total += input;
average = total / count;
}
 
printf ("Total = %d, Average =%d", total, average;

You'll have to add some errorchecking for the input etc, but I hope this helps.

Yuck. And bad psuedo-code too. Psuedo code should not be written in C/C++. And haven't you ever heard of formatting?

set count to 0
set total to 0
ask for input
while input <> 0 
{
    increment count
    add input to total
    ask for input
}
calculate average from total and count
 
display total and average

If you're going to use a while loop, use it as intended unless you need an endless loop. And don't waste time calculating the average inside the loop.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Create your program as a Command Line program, or whatever your compiler calls it.
Build it creating an executable.
Open a command prompt window Start:Accessories:Command Prompt.
Navigate to the directory -- sorry, folder -- the executable is created in.
Enter the name of the executable.
IOW -- You edit and build the program in the IDE, switch to the prompt to run the program.

Or you can run the program from the IDE and a command line window will open. But you have to stop the program just before it exits because the window closes immediately. getchar() is probably all you need. I usually don't recommend this technique because of the added code to keep the window open.

Third option is check the documentation and see if you can run the compiler from the command line. Use an editor to create the source, use the command line to compile and run the program. Personally, my preferred option.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Is this an example of a recursive function?? Somebody on another forum helped me put this together, as I am trying to make a diamond shape out of asterisks. But he did not know if this was a correct way of using recursion. Can somebody just tell me yes or no, is this a correct example of a recursive function?? Thanks

No.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

All the expert, please help me, because my lecturer said this question is difficult, and nobody will do this. so, i want to proof to him that still got expert can do this question...Thanks

And neither will we. I did this for my class 35 years ago, I don't need to do it for your class. But if you attempt it and post your problems, we will attempt to help you. But you already know this, because you did read the post above titled Read Me: Read This Before Posting, right? :icon_twisted:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Personally (and I'll get slammed for this :icon_wink:) I'd use printf . It's designed for formatted output, rather than that convoluted cout mess.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You need to describe the error. No one is going to spend an hour trying to find the error, then pour over your code to fix it. You need to describe the error and the circumstances with which it happens.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I guess I can't argue with carefully contrived examples.

Good... Because this is exactly the type of input that has caused massive brain hemorrhages in students on help forums. :icon_smile:

Not just scanf. If I don't know for a fact that the input will be formatted correctly each and every time. It looks like you want me to ignore scanf, sscanf, strto*, ato*, and any other existing parsing mechanism in favor of my own hand rolled application-specific parser.

I never said ignore them. Only gets(). And if you noticed, I also suggested using sscanf() . I personally don't like strto*() functions. But I do use ato*() functions, even with their potential traps. And that's because in those programs I've already validated the input and know the functions won't cause problems. You simply can't validate input with scanf() . But you can with sscanf() .

I think you got mixed up somewhere along the line. I'm not trying to convince you that scanf is suitable for user input. I'm addressing your attacks on scanf by showing you that it's the user of the function and not the function itself that's the problem.

Two responses:
#1:
Agreed. And my point is that students are users that simply do not understand the function and instructors should either:
1) stop teaching it early
2) explain it has problems and use it sparingly -- if things don't work you've been warned
3) teach the darn thing! Give the student a …

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Yep. You're at a disadvantage alright... :icon_wink:

What I can do for you is give you a sample of a recursive function you can look at. It's up to you to think through the changes for your program.

#include <stdio.h>

void recursiveFunction(int val)
{
    // The working area of the function
    printf("called value is %d \n", val);
    val--;                          // decrement val

    // The test of the end-value
    // In this simple example it's simply waiting until val becomes 0
    if (val > 0)
    {
        recursiveFunction(val);     // >0 call again
    }
    else
    {
        return;                     // <= 0, done
    }
    
    // Returns will come here and val will be restored to
    //    it's previous value
    // Do the next 'work'
    printf("return value is %d \n", val);

    return;
}

int main()
{
    recursiveFunction(5);       // start the function off
    
    return 0;
}

Output:

called value is 5
called value is 4
called value is 3
called value is 2
called value is 1
return value is 1
return value is 2
return value is 3
return value is 4

PS: Hope he'll be better soon. And I hope he's doing most of these projects... :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I didn't realize you were the one who wrote those articles. :)

Surprise!!!!

So is printf for a lot of ways it's used. I don't see that as a flaw in printf, do you? :)

I didn't say it's a flaw in scanf(). Reading a char is necessary and useful when reading formatted input that contains varied field types, including char. But for reading only 1 char, that's my argument. Just as using printf() to output only a single char is IMO a misuse.

If you're using the function incorrectly, and then kludging up your code to fix it, I'd call that sloppy.

Given what a student programmer generally knows, how is using scanf() to read an int and leaving the return in the buffer sloppy on the programmer's part? Code must be added to clear the buffer or a convoluted format specifier must be handed to them. And I'll bet many, if not most, instructors have no idea how to clear the buffer using scanf() while reading a number. Especially with ill-formatted input via user-error.

Why is that an error? Did it occur to you that the R might be there in anticipation of the next input request? One of my biggest pet peeves is code that always clears the line to remove "garbage", when what I typed was most certainly not garbage.

Since I came up with the example, you can be absolutely assured that the R in "23R" is an invalid input, not meant for the next …

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

For starters, you typed right over the instructions on using code tags to make the code readable, and obviously didn't read the post titled "Read Me: Read This Before Posting"...

The recursive function must:
when called,
1) output a specified number of *s and SPACEs.
2) test if the value is at a defined end value
2a) if not, call again with the next value
2b) if so, return

when returned
1) output a specified number of *s and SPACEs exactly as above
2) return

That should do it...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

The above post started a scanf() discussion, which has been moved to http://www.daniweb.com/forums/thread94021.html

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

>I disagree. You have to have all kinds of special code to get scanf() to work properly.
Only if you aren't using scanf for what it was designed in the first place. When you have to write workaround code to get a function to work, you're using the wrong tool or using it the wrong way.

My point exactly. scanf() is dangerous and inadequate reading strings (only reads to whitespace, and can easily blow your buffer if no spaces are in the input)
It's overkill if you want to read a single character. Why bring in the code for translating %d, %i, %f, etc. when getchar() does exactly the same thing in a small footprint?
After reading a number, the input stream still has characters left and must be cleared if reading strings next. This requires "fix-up" code.

I am of course targeting those that learn scanf() in school. Instructors never seem to explain any of the problems associated with the function, letting their students flounder. They are not taught the formatting characters required to use scanf() cleanly. That's why all the forums are inundated with questions about skipped input, partial input, and looping input.

I'm not sure if he's being humble or really hasn't studied scanf fully. As some background, I, on the other hand, have studied it completely for many years, and also implemented it completely for both C89 and C99 several times (each in a different way). I like to think I have …

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Details. What works? What doesn't? Specifically what is your problem area?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I have made a few changes but it really still does not do wat i was xpecting

That's too bad. Unfortunately most of us aren't going to play with the program to try to figure out what it's not doing. You need to tell us what it's doing wrong. This isn't www.psychics.com you know...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Salem's request is to repost your code with formatting and Code Tags... When we can read the code, we can help.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Define a value for each of the 8 directions:
1-up
2-down
3-left
4-right
5-up/left
6-up/right
7-down/left
8-down/right

Then get a random number to decide which direction to choose.
Randomly choose a starting location and see if the word will fit
1) enough space in the chosen direction
2) any letters already set must not be changed.
For #2 you may have to modify the starting location to make the word fit.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

>Haven't you figure out yet why you should avoid using scanf?
I don't think you should avoid scanf. I think you should learn how it works so that you can use it intelligently.

I disagree. You have to have all kinds of special code to get scanf() to work properly. It's different for ints, char*, and single chars (which is a total waste IMAO). See this series.

>Reading an integer from input requieres from you the programmer to know how it works.
Isn't that the case with everything? I mean, if you don't know how something works, how are you supposed to use it the right way?

You hit the problem right on the head. No one teaches the right way so everyone struggles and asks the same question program after program.


Best to switch to fgets() and be safe. Then for formatted input use sscanf() to break the input apart. But in your case, megan-smith, you need to look at the input and check if there are illegal characters before using sscanf(). That way if someone enters 23x the input fails as it should.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

if (islower(x)) can be written if (x >= 'a' and x <= 'z')

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Seems to me you can't actually solve the equation without some values. Therefore, ask your instructor for more details.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Start by writing a program that accepts parameters on the command line and prints them out.
Once that's working, check the parameters to see what they are and output messages for each so you know you are analyzing the parameters correctly.
Then open the file, read it, and display it.
Basically, do a piece at a time...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Each letter (in fact each character) has a specific numeric value. Each character can therefore be used as an index into an array in which you can increment the appropriate element. Then run through the array when done and output the values you need.

Unzip and run the attached program for an ASCII chart.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You should not be opening the same file for reading and writing. Use fout to write each line read (changed and unchanged) to a new file. When done, delete the old file and rename the new file.

Also, read this about .fout() (which is identical to feof() .

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

If you want to store mixed data than I'd use a structure that has two members: (1) a union between all the data types you want it to support, and (2) an int that indicates what kind of data is stored, such as 0 no valid data, 1 is an short, 2 is an unsigned short, 3 is a int, 4 is an unsigned int, etc. etc.

... keeping in mind that you have a problem if you want to add strings too. Think very carefully if you need them.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

#1) You can't use strcat on strings. Only with char*. Look up string methods.

#2) Break the following line into pieces to create the file name and output it so you know it's correct before using the open function. It's also less confusing. infile.open(strcat(strcat(st1,(strcat((string)i,st2))),ios::in) #3) Read this about the following line (.eof() is the same as feof()) while (!infile.eof()) #4) Also read this to find out how to format code so it can be understood.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

How many threads are we working with for this program? Maybe you should have only one thread so no one gets confused. Especially you.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

hmm...u dont know the arrays part...
i think u cn try this..
. . .
hope dis is of sum help

sorry i did sum mistake in d above program...
below is the corrected one n its working perfectly...
. . .
i hope it is of sum help...!!!

Trace, two things:
1) Read this so people can understand you
2) If you give people the answers to their homework, they learn nothing. Please help them find their own answers.