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

1: Zero your Total
2: Read the input as a string (hex)
3: Check each character and conve rt it into decimal value (A=10, B=11, etc)
4: Multiply Total by 16 and add the above value
5: Back to 2

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

I'm considering allocating the space on behalf of the caller as a bad practice (except malloc in C, but no need in malloc in C++ ):
1. It's annoying and error-prone to deallocate returned memory on behalf of the callee. It's a narrow pedantry to count this tiny buffer size in relatively expensive (operator divide) loop. As usually, there is some minimal size of an allocated heap chunk in C++ implementations (16 bytes or ever more) so you can't save actual memory expenses.

Exactly! What I said about malloc() in my first post goes for new too. STOP USING IT! Reread what I said.

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

isnt gets() almost the same as scanf()? im using it because it gets more characters than scanf().

Yes. See this and this. Stop using gets() !

C code works inside C++ right. we use C++ in class and i dont think thats a problem, for me.

Then write C++ instead of C. If you want to write C code you must follow the rules of C and stop arguing with those that know the rules.

aiight, dude. i'm tired of all this grabassery. here's standard c, that will actually work.

He finally got to you... Oh well. :icon_wink:

Salem commented: Saying "Noooooooo" to gets() :) +29
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hello everyone, I'm working on a small assignment where I am supposed to write the code for the itoa function char* itoa(int Value, int Base); without using any built-in functions, and where the returned value is allocated on behalf of the caller. Value being the integer to convert, and base being octal, decimal, or hex.

I found this rather simple implementation and I was hoping we could discuss it here to see if the changes I'm making are appropriate/accurate and etc.

OK, have a seat...
1) malloc() -- this will cause memory leaks on each call. Once the function returns, the memory address allocated is lost and can not be returned to the heap. Stick with the static char from the original code.

2)

for(int i=30; val && i; --i)
  {
    val /= base;
    buf[i] = "0123456789abcdef"[val % base];
  }

The first thing you do in the loop is remove the low order digit so if you pass in 2765 you would get back 276.

3)

buf[i] = "0123456789abcdef"[val % base];

This is a trick that I personally would avoid -- but that's just me. Create a char* with the character values.
Also, what happens if the base passed in is > 16? This line will cause a lot of trouble. You need to test for proper base range to protect the program this function is used it.

tux4life commented: Nice posting, nice advices to the OP :) +2
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Regrettably, there is no a little thing in <ctime>: guarantee of 32-bit integer time type.
The time_t type is an arithmetic type capable of representing times - that's all...

I don't understand... Is there a problem with ctime?

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

OK, fine. It's generally the last thing a professional programmer would do.

Peeking is generally used for OS level programming when you need to do things outside the ordinary. Reading the character and processing it is ordinary. And this task certainly can be done easily using the ordinary techniques.

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

My ghod! Don't use cin.peek()! Read the line as text and process it.

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

Look at the header ctime. All the time functions are there. A Google search should give you definitions and explanations.

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

What integer is '|'? That looks like a character so you cannot read it into an int variable.

To do what you need you'll have to read the input as characters. If you get a number, convert it to an integer. If it's not a number, do what you need with the character.

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

You opened filein and you're reading from cin.

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

you mean the index = 0;
i seriously don't know what that means. i have seen it before but i don't know what it does
and which loop do i use? For loop?
and how do i add the contents of my array

If you don't know what index = 0; you seriously need to start reading your book before continuing. I suggest you talk to your instructor...

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

Because we already got our grades and passed the course. You can't pass using our work. You need to pass on your own work.

Salem commented: Well said +29
tux4life commented: Well said :) +1
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What you are attempting to do can be done with arrays.

Imagine you have an integer variable called "number" with a number, and when you executes a function, this function creates a variable called "audio1" and increments number (number ++; ). The next time this function will create another variable called "audio2".

With audio as an array, you can reference audio[1], audio[2], etc.

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

Start by defining two arrays, first to hold the values and second to hold the vowels themselves. Then use loops to check the input characters with the letter array and increment the correct value in the other array (or define a 2D array to make it easier).

Then for the sort be sure you sort both arrays at the same time. Don't use the sort() function, but sort the values by writing your own code.

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

If menu() returns a value, then return the value. It should be a char , not void , function.

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

BTW, char chr_digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\0'}; is same as char chr_digits[] ="0123456789";

True. But based on the code you don't seem to need a string, just an array of characters. So char chr_digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; is the 'correct' definition.

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

Well, I want to know how to insert a full string into the array of strings. I thought it might be:

fscanf(infile, "%s", *arr[i]);

but this line gave me the error "warning: format argument is not a pointer (arg 3)".

No. It would be fscanf(infile, "%s", arr[i]); But, see this about scanf/fscanf. It would be better to use fgets() instead.

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

Yes, it is cool side, but I see no reason to spam around the forum without reason...

One post IMO is not "spam[ming] around the forum"

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

The problems I see in that function:
1) using getch() -- see this.
2) recursively calling fnAddDepartment(); because an invalid department entered. Should simply loop back to reenter the value.
3) Your formatting does not make it easy to read the function. See this about formatting.

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

You said the temperature in the file is in the format
C23.4
F74.7

Where did you read the C or F?
Where did you test the C of F to decide if you need to convert?

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

Your shuffle seems backwards to me. Assuming card values mean something like
1 = 2 of clubs
2 = 3 of clubs
...
13 = A of clubs
14 = 2 of Diamonds
...
52 = A of Spades

I'd change the shuffle to something like this...

Fill an array with 1-52 representing an unshuffled deck
Get a random number between 0 and 51. This represents a position in this array.
Move that value to your deck array. Just go from one to the next in deck.
Remove the 'used' card by moving each and every value after position into the previous location (value in position+1 into position all the way through the end of the array.)
Do it again but this time getting the random number from 0-50 (only 51 cards left.)
Keep repeating until you've moved each and every value.

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

I want to ask them with a prompt, "What day is it?

I then want them to enter Sunday, Monday, etc.

I used %c because I was trying to reading the name of the day, which would be a set of characters.

No, you don't want this. At least not in total.
First, %c reads a single character
Second, %s reads a string, but you don't want to use it. Here's why
Third, it also makes your code much more complicated. You have to read in the string, then convert that string to the correct integer. Instead, how about prompting: What day is it (0=Sunday, 1=Monsay...)?

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

Come come people. Look at the code. You can't read an integer using %c

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

maybe variant argument list will do ...

what do you mean xeuron?

Something you don't have to worry about. Use Dave's idea.

Also, read this.

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

It also might help if I tell you guys the whole program (*smh*...lol):

It also might help to tell us what the problem is and what you're trying to accomplish.

If this is the same problem as another thread, you should have just continued there.

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

After your cin >> choice; add code to read the rest of the input buffer to clear it out, up the and including the \n.

When you enter values, ALL characters you type are in the buffer, including the <ENTER>. When you read a single character, the \n is left in the buffer for the next read.

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

Counter-question: what for start label and goto?

for (;;) { // loop forever: it's a clear and honest construct
...
}
// or (I don't like this)
while (true) { // longer and precious
...
}

I agree that you should not be using goto . And I have no idea why a while is precious, and longer.
I prefer the while() to the for() -- it's more logical to me.
The argument for the while() is a TRUE or FALSE expression, which true fulfills exactly.
But for() is generally meant to loop a specific number of times, and leaving out all 3 expressions seems to be a kludge to get an endless loop.

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

don't use the return result of eof() to determine whether the body of the loop will be entered.

Why is it people just say something without explanation? Why should they listen if there doesn't seem to be a reason?

Lerner is correct, and here's why. feof() is the same as .eof()

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

Ahhhhhhhhhh! Thanks for the clarification on the memory issue and FIFO naming! So, if I instead assign two pointers to char to point to argv[4] and argv[3] THEN concat, I should be alright???


OR... not.. :-(

Not. Reread Salem's post, specifically

... if you want to extend an argv, then you need to copy it to somewhere which has the space to append.

And to explain why he said "2) You're using gets(), the world's most dangerous function", read this

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

Also, everyone says to avoid goto statements. Can someone tell me alternates to using goto? Like could you give me an example of where I used goto and show me another way.

Sure. In general:

do
{
    [I]display menu
    input [B]MenuValue[/B]
    process the menu entry selected[/I]
} while ([b]MenuValue[/b] != ValueOfExitChoice);

Also, you badly need to read this. Most people will not be able to follow your code making it hard to get help.

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

What he's getting at is variables should be defined only in code files, the .C or .CPP files. NEVER put a definition in a header file. Only the extern goes in the .H file.

The guards are only useful when a header file is used twice during a single compilation. When you compile 2 separate sources individually, the headers files are processed as normal and each object file gets the definitions specified -- both sources will have currentpacketbeingsampled defined.

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

I wouldn't do it. Flashing a message in different colors is one of the most obnoxious things you can do to a user IMO.

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

You've obviously never had friends.

Ahh, when someone points out why your advice is lacking, you go to personal attack. I've been soundly trounced! :icon_rolleyes:

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

i made it upside :P that's not giving away the code.. right?

Debatable... :icon_wink:

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

I've looked at your post for about 5 minutes and have absolutely no idea what your question is about! OK, you're having a problem with a line. The line is only for reference -- whatever that means. Where do you explain what the problem is? Or what happens to show you it's wrong?

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

The reason I'd code it w/o a loop is because it would take less time to hard code a few lines of '*' rather than spend time to think about the pattern then write loops based on it.

You've obviously never passed a programming course.

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

No problem's here, either. Fox 2.0.0.3

Midi, you seem to be having problems no one else ever sees. Could your OS be damaged in some way?

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

Hello..... I want to ask you something..... My teacher gave us our final exam last October 16 & 17.... I want to ask you about this question: Create a program that could show this output
*
***
****
******

If you'd try reading your post, you'd see that the question will never give you the proper answer. Your triangle does not give us the actual shape. Had you pressed PREVIEW you would have seen this. Wrap any lines you want to keep the proper formatting between CODE tags.

for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("*\n");
}
}

Before posting code, format and test it first. You should never post working code as an answer to anyone's question (help them find the answer instead), and NEVER EVER post code that flat out doesn't work -- as your code doesn't. Twice.

There are a lot of ways you could produce that code using while loops or for loops, but most of them should not be coded using a while loop or for loop...

And what would you use? A switch statement? Don't just say something controversial (wrong), back it up with a reason. I for one always thought a loop is the absolute best statement to use to do a repetitive operation.

-------------------

Loop #1: Loop for each line.  Within this loop (nested is the term)
    Loop #2: Loop to print the spaces before  the *s (inner loop 1)
    Loop …
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
strcpy(input,"          ");  // You don't need to do this if your next 
                                   //    line overwrites the value
   	scanf("%s",&input);        // see this
   	fflush(stdin);             // see this
Prabakar commented: nice links +1
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

All you need to do is set up a do-while loop and read the input inside the loop.

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

You need to test the value after reading it. If invalid, start the loop.

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

Did you call srand() only once? Or every time you called rand() ?

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

I have the same question as Salem -- why are you reading one character at a time? And if you are passing input into the function, why are you not loading it for the return?

Also, FYI, see this and this about scanf() . Why instructors insist on teaching bad techniques I still have no idea. Maybe someone will ask sometime and post the response.

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

Where did you use the variable index to insert the variable insertItem into the array?

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

Use continue instead so that you can enter the salary again...

i = 1;
	while(i <= numb_salaries){

		printf("Enter salary #%i: ",i);
		scanf ("%i", &salary);
		if(salary < 0){
			printf("\n Please enter a valid salary...\n Press enter to re-try...");
			fflush(NULL);
			getchar();
			continue;
		}
		sal_total = sal_total + salary;
		i++;
	}

Although the continue works in this situation, there are many times it won't work, so Aia's solution is better.

And what does fflush(NULL) do?

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

There is a difference between a character array and a string. A character array char a[3] = {'a', 'b', 'c'}; is not a string. Just because it happens to end in a '\0' when you don't define all the elements char a[3] = {'a', 'b'}; does not make it a string. Don't use it as such.

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

To get ths output u will have 2 flush the input buffer after every char input

Enter Gender Male/Female(M/F): M
Enter Residence City/village(C/V): (it'll not wait here to take i/p & jumps on next line)
Enter Health Excellent/poor(E/P): E
Enter Age: 25

printf("Enter Gender Male/Female:");
scanf("%c",&gender); //gender is char type variable
fflush(stdin);
printf("Enter Residence City/village:");
scanf("%c",&residence);
fflush(stdin);

and so on :)

Absolutely the wrong way to flush the input buffer. See this.
And also a terrible way to input a single character. Here's why.

The concept is correct, though. Instead, use a loop and a character input function:

gender = getchar();
while (ch != '\n') ch = getchar();
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

what should be done to get the input from normal arrow keys? (not the ones on the num-pad) Do they have different scan codes?

Why not write a short test program and find out. You have enough information to do that.

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

Each function and arrow key is really two characters, a \0 followed by another character. Note the code:

KeyStroke = getch();
if (KeyStroke == 0)
{
    KeyStroke = getch(); 
    switch (KeyStroke)

Get a keystroke with the first ]getch() call
If that value is a 0, get the next keystroke...

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

Maybe for a member's first 5 posts a message displays with the high points of posting and what may happen if ignored.