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

There are a few thing wrong with your code :
- you're incrementing 'i' twice in your word_count function. That's not good. You're missing characters because of this and you'll probably run out of array-bounds.

Not really. There loop only increments i . That's the only real problem.

- Why use isspace() when you can just say: if (str[i] == ' ')

Because isspace() will work with not only a SPACE but TAB, and the ending \0. As well as other characters. It's a much better way to go.

- and very important:

while (str[i] !='\0')
i++;
{
	if( isspace(str[i]) && !isspace(str[i+1]))

What happens when i is at the last character and the code in red is executed?

Nothing. The loop ends when the end is reached. Compares the last character with \0, which is part of the string.

Also it will still tell you one less than the actual number of words in the sentence. Because you are counting the spaces not the words. eg

Nope. Not with isspace() .

>This looks like a C-code more than C++ and should have been posted there.

Really? i just started taking up computer this sem so i cant tell the difference. I posted it here because the program that our teach lets us use is Borland C++ 4.5

If you are taking a C++ course, the code is C++. It's just primitive. No one can expect you to know the difference if your instructor hasn't explained the difference.

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 Firefox help site says it is probably an add-on some website installed without my permission.

If this is true, what can DaniWeb do about it?

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

getline will read the entire line, then you'll have to break up the line read into its individual strings.
Alternatively, you can use >> to read in each individual string. >> will stop reading at whitespace.

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

To expand on what vmanes said, create a 2D array, something like inputbuffer[4][250] (4 lines, 250 chars each).
Reading each character, load the characters in the array.
When you reach the end of one line from the file, add a \0 character and point to the beginning of the next buffer line.
When done, you can output each line using puts() .

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

Not a clue what you are talking about.

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

What you need is a tutorial that you can do at whatever speed you can handle. This post may help you find one suitable. Or the thread on books.

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

Who says the 'buffer on the stack' is formatted properly for sscanf() ?

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

The only correction I can see, and it's minor, is that postconditions is not a valid English word.

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

You were asked to use code tags which are described
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used, even in responses to your posts
6) on the background of the box you actually typed your message in

Did you bother to read any of the suggested posts and rules?

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

* You also didn't bother to read the rules as requested multiple times during registration (code tags and the title show that)
* you didn't format you code so people can read it
* you somehow think people are here just to bail you out of your problem and don't have lives outside of this forum
* and as devnar implied, you don't know the basic syntax of C and expect us to teach you the language. That's not why people volunteer on this board.

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

Making a change to RF's equation will give you the ability to get random numbers in any range. Add a start and a range variable: ((int) (range * (rand() / (1.0 + RAND_MAX))) + start) So, if you want numbers from (and including) 3 through 10, start is 3, range is 8
For numbers from -100 through 100, start is -100, range is 201

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

scanf("%[^\n]",str); Do you want to bet I can overflow the 15 characters that str[] can hold?

Wat u said regarding the overflow of string array is perfectly right...but my question is can such overflow of array's at such subtle level cause major damages.? if yes how.? and what to refer to avoid such minute but important details...? I mean to brush up the coding skills...

#1: wat u is not English. Brush up on the forum rules you read when you registered. We speak English on this board, not web/leet-speak

#2: Yes overflow can cause damage, and it's not subtle at all. If your buffer can hold 10 characters and you type in 25, there are 15 bytes that just overwrote data -- ints, floats, and other chars now have data in them they shouldn't have. Use fgets() as suggested.

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

Now I'm completely confused. In your first post you state

am trying to append to file but its not going correctly

The problem is you open the file for append (write at end) then try reading from the file. That's just backwards.

Then:

but what if I want read from the file ?

Open the file for reading, not appending

how can I delete a line from the file ?

You can't -- directly. You need to read the file line by line writing each line to another file. When you get to the line you want deleted, just don't write it. You basically end up making a copy of the file without the lines you want deleted.

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

try this out i will work i used fgetc, iguess you should know that its interger not some string to store in.

What? :confused:

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

Usually you change the buffer in memory, then write the whole buffer to the file after all the changes are made. Don't try changing the file as you change the display.

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

My guess would be that this loop

for(loopcounter=9; loopcounter!=NewRank; loopcounter--)
      {

exits when loopcounter equals NewRank so

if(loopcounter==NewRank)
        {
          ListToBeChecked[loopcounter].TimeTaken = Time;
          strcpy(ListToBeChecked[loopcounter].Username,UsersName);
        }

is never executed.

Also, see this about fflush(stdin); and this about gets(UsersName);

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

That's how sscanf works. A decimal is illegal for integers so the translation ends. You'll have to reconsider how you read/translate the file.

Also, consider these problems:

see this about void main() You've defined c[] to be 10 characters. How many characters are in the first line for textfile.txt?

And see this about getch()

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

You need to search out the binary AVI file format and read the file header. The length should be in the header.

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

try ./filename

What does this do?

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

Don't you know the universal icons for... for... heck, I don't know either!!!

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

Create arrays for each hand and put the deck's first card in the first hand/first card, deck's second card in the second hand/first card, etc.

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

So true. So you really need

char buff[BUFSIZ];
fgets( buff, sizeof buff, stdin );
if (buff[0] == xval)
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Never Ever call main() as you are doing in your return statements. Use a loop. Ony the operating system should be calling main() Use a lot more whitespace to make your code easier to read. In other words, change

if(strcmp(choice, "y")==0)
math=diff/i*1000;
cout<<""<<x<<" more tries"<<endl;

to

if (strcmp(choice, "y") == 0)
math = (diff / i) * 1000;
cout << "" << x << " more tries" << endl;
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

This series might give you some insight on scanf() .

And why would you want to use fgets() on stdin to read a character? That's what getchar() is for.

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

i have three problems first one is in vc++ 2008 express heres the code...

And the problem is....?

Second is in old turbo C++ 1.01
how can i use a similer to if ( X = X) for char instead?

The exact same way you do it in VC++ 2008. Either that or I don't understand the question.

and third how do you erase a graphic element in turbo C++ 1 without closing graphics?

Depends on how you created the graphic element. First thought is to rewrite the graphic element but with the background color.

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

1200 lines of uncommented and badly formatted code. No indication what the warnings are. No indications where the warnings are. How can we possibly help you? Read each line and try to figure out what lines might be the problem? Try reading the Guidelines.

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

it didnt have a very long lifecycle?
IE gets years and years...

And it shows :icon_twisted:

~s.o.s~ commented: LOL :-) +25
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

About scanf ("%c",&opt); -- See this information...

"}while ((opt!="1") && (opt!="2") && (opt!="3"));" is okay because it forces the input to be 1|2|3. For a menu in c it is okay.

No it's not. "1" is a string but opt is a character. Okay is }while ((opt!='1') && (opt!='2') && (opt!='3'));

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

Your best bet is to press "M<return>" and use fgets() to read the input and process the first character. It's portable and will work on ALL compilers.

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

I've made a program using gcc as a compiler.

In my loops I've been using a uint type as the type of my loop variable.

But now it seems that some systems doesn't support this type.

Can I make a preprocessor conditional that does a

#define uint to unsigned int

if typename uint doesn't exist

thanks in advance

Why? Use the standard unsigned int rather than a type that doesn't exist in most compilers. You've now seen the reason we try to always recommend not using compiler-specific enhancements.

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

First, you need to learn how to format your code. This will help with following the code -- for you and us. If we can't follow your code, we can't help much.

What is the calculation for calculating the interest for one month? How did you arrive at 25.64? If it was given to you by your instructor, how did he arrive at the value?

And while we're at it, check this out, too. Something you might want to ask your instructor about.

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

The book is "Who Moved My Cheese?" by Spencer Johnson.

... It is really good you changed the layout, ...

Sounds like a Twilight Zone story with Billy Mumy :icon_twisted:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
char a[];//random string that phonenumber is compared to

The variable a as defined can hold 0 characters. Therefore there is nothing to compare it to. If you try to load anything into it, you will overwrite some other variable or crash your program. Give a a size.

Can you give examples of what I can do to improve my writing?

Examples are in the link posted.

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

Start your "unique number" at 1 and increment it for each new data record. That seems to be the easiest and safest way to make all your numbers unique.

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

By reading each character one at a time. If you get to 5 and that character is not '\n', print an error and clear out the rest of the characters from the input buffer.

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

Here, let me help you with the gets()/fgets() question:
Click Here.

And I'm sure it's inevitable, he's going to do the same with scanf() , too, so:
Click Here too.