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

Also speak English, not pigeon-speak. And use CODE tags. They are described everywhere on this site.

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

hay dear i need help for c prog...
i used <textcolor (14+128) ,cprint> commands to blink some text. befor i drow rectanngel and some circle as back image. but the text is not apper. if i closed the graphic mood, then it'll show. but i need kept the graphic back ground. & i wanna to know how to gave input text in graphic mood. bcz i tried <scanf> it's not work propely under d@ situation. i hv 2 used only c, for d@ task.
pls help me!

bye!

Hay dear, plz rd the rulz. Esp da 1 abt "Keep It Clean". d@ wil hlp. Da rst uv da rulz iz imprtnt 2.

devnar commented: Laughed out loud! :) +1
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

would please correct it for me and posted in this forum, It's been 4 hours I've working on this but it doesn't work.

All you have to do is count!!

#include <iostream>
#include <cmath>

using namespace std;

void Instructions();
float do_next_op(char op, float o, float
 CAV);
void startCalculatorProgram();

void main()
{[B]  1[/B]
	startCalculatorProgram();
}[B]  0[/B]

void Instructions()
{[B]  1[/B]
	cout << "Begin Calculater " << endl;
	cout << "Instructions are:" << endl;
	
}[B]  0 (so far so good)[/B]
float do_next_op(char op, float o, float CAV)
{[B]  1[/B]
	switch (op)
	{[B]  2[/B]
		case '+':
			return CAV + o;
			break;
		case '-':
			return CAV - o;
			break;
		case '*':
			return CAV * o;
			break;
		case '/':
			return CAV / o;
			break;
		case '^':
			return pow(CAV, o);			
			break;
		default:
			cout << "the input you choosed is not correct" << endl;
	}[B]  1[/B]

void startCalculatorProgram()
{[B]  2  (2?? This should be a 1! Something is missing above)[/B]
	Instructions();

	float accumulatedValue = 0.0;
	char op;
	float o;
Salem commented: Nicely put +23
Ancient Dragon commented: exactly! +36
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You might be able to use the built-in functions ispunct() and isspace() to 'tone down' some of the long windedness.

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

after entering an integer the program leaves the '\n' in the keyboard buffer. When the next getchar() is called it gets that '\n' from the buffer instead of waiting for user input. To correct this add getchar() after scanf("%d", &consump);

See the problem is due to the 'New line' character...

Gee, didn't AD already say all this? :icon_rolleyes:

Only plain wrong if you're not using a compiler that has defined it.

:icon_rolleyes:

As usual, there are some exceptions, check your compiler's documentation to see if it has a (non-portable) method for flushing input.
Ya its right that using fflush(stdin) is not 100% correct or what we can say, not a perfect coding.

"OK, I used your suggestion on my Lame-C++ compiler and my computer crashed! All my data is now gone! Maybe you should have told me it only works in your compiler! :icon_mad:"

Also, remember one thing there are too may ways of writing a code for the same purpose. Consider an example of for and while loop. They are nearly same but used as the choice of programmer.

Not to the professional -- or someone that understands how to code. They are both different and should be used for the appropriate job.

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

Salem is right...
String is uninitialised... you get junk for len... for sure...

Then why bring it up. He already had others confirm this. :icon_rolleyes:

Yes, EOF is an issue... but while entering the character where does EOF comes into picture?...

Anytime you pipe in a file or press the EOF keystroke.

This thread is a great examply why people need to stop giving working answers to questions. Lots of code that teaches bad techniques. Just help the original poster find his own answer with hints and suggestions, not poorly designed code.

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

They differ a lot, since turbo c++ compiler is ANCIENT.
There is something that is called standard C++ language. It wasn't fully defined at turbo c++ time, so some things that were differently defined don't work nowadays.

But, for a new student, these differences can be minor. I rarely have a problem switching from one to the other. (maybe I just know too much)

And try not to use c++ compilers, although they should work with C :)

I am unaware of any compilers that are C only designed in the last 18-20 years, especially based on the recommendation to get a new compiler. Virtually all compilers today are C++ compilers and, since C++ is (guestimate) 75% or more C, they all compile C without problems.

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

As per you, it is C, then this is the right forum...
And what makes you say, its c++??...

The actual questions deal with the calculations, not the output statements. They are fully C++ as well as C. And printf() is a C++ function. Check any C++ book/tutorial/standard.

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

It's called a depth-first search since you know the maximum number of moves.

Start with the unsolved starting point
3 5 2
8 7 1
4 6 9
Then generate the first possibilities with upper-left
1)
832
751
469
saving this solution
Then rotate again,
2)
782
351
469
and again,
3)
372
581
469
and again,
4)
532
871
469
You are now at 4 moves with no solution.
Back up one move (to 3) and try the upper right corner, followed by the UpperLeft again.

IOW, you cycle through UL, UR, LR, LL each time you back up. And each time you back up, start again with the UL. Kind of like trying to solve a maze.

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

When you read a file that's a directory, call the function again. And learn to use
CODE tags. There are at least 6 places on the site they are explained.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
char decision;
int flag = TRUE;
while (flag){
        /*do some stuff here*/

        printf ("Do you want to continue (y/n)\n");
        scanf("%c", &decision);
        /*be careful, \n stays in input buffer*/
        if (decision == 'y') flag = TRUE;
        else flag = FALSE;
}

What a gross input statement. See this.

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

And posts should NEVER be marked urgent! It's not urgent to us -- we'll get to it when we get to it. And many people simply bypass threads titled like this so you've just cut down your chances for help.

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

stilllearning is correct. The compiler is only interested in the statements that do something so comments are not part of the compiled program.

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

It would be nice because then I could do the following:

for(int i=0, double d=1;i<5;i++,d*=1.5){cout<<"1.5^"<<i<<"="<<d<<endl;}

This would make for cleaner code as it is clear that i and d are used only inside the for loop.

I disagree. That to me is confusing and unclean.

On the other hand:

for (int i = 0; i < 5; i++)
{
    double d = 1;
    cout << "1.5^" << i << "=" << d << endl;
    d *= 1.5;
}

This is much easier to read because
1) i is the only variable important to the for statement itself
2) d is defined to be used only in the loop
3) formatting and whitespace makes the code much easier to read

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

On the second page of the thread is a small testing program and some results showing that I only had the problem using fread() on "text" opened files, not on binary opened ones.

You obviously missed Narue's answer to this problem:

size_t n = fread ( buf, 1, 4, in );

[B]    // fread doesn't append '\0'
    buf[n] = '\0';[/B]
    puts ( buf );
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Now read the information
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) on the background of the box you actually typed your message in
to learn how to use code tags. It's not like the information isn't available and in your face. It's really hard to miss.

Salem commented: You forgot to add "don't provide complete answers on a plate in response to zero effort" +22
iamthwee commented: yup +17
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Anyhow, here is my code and the errors are C2676 and C2784 in Visual Studio

Since we are humans, not compilers, C2676 and C2784 mean nothing. And even less because we don't know what lines caused the error.

Details, me bucko, details. They are important.

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

I thought you wanted to read from a file or stdin. If this isn't true you might want to give us all the information we need to understand what you're trying to do.

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

Your sort is close. See this to improve the sort. Hint: Your loop values are slightly off. Otherwise, good job!

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

Yeah, that's what I meant... Esp since it was pseudo code, not C code.

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

it is known that dennis rithie made c with only 32 keywords and writing functions is the responsibility of the programmer. who wrote the functions printf, scanf?

Usually the author of the compiler that has the functions defined.

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

I used the dev-cpp debugger to add a watch to the variable keystroke and found out that when i pressed the uparrow, the first character wasn't zero, but some negative vale. So by changing the code from if(keystroke==0) to if(keystroke<=0) , i finally got it.

Thanks WaltP

That's very weird. What value (HEX) was it? Maybe 0xE0? By specifying <=0 you may have turned some legitimate keys into function keys. There are some keys that actually have a negative value if you program the code wrong.

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

Good evening.....

I just wanted to share an idea.....can this forum be downsized into categories such as API,FORMS,CONTROLS,DATA REPORTS and so on.....the reason i am saying this is people are lost in here....

Who besides you is lost? Have you tried Hare Krishna...:icon_twisted: (see The Muppet Movie)

Nick Evan commented: :) +9
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

PM your address and I'll mail you a copy of the dvd (yes its a legal copy, I download the ISO fle and made my own dvd).

Wouldn't it be faster even with dialup to download and burn the ISO instead of waiting for the US Snail?

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

Or,
Read in the numbers into the array
Sort the array
Then the duplicates are easy to find.

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

I have to take a 2d array of [6][8] floats that represent a star map and add the four surrounding numbers to it to measure it's intensity of light. Would something like make sense???

for(i=0;i<6;i++)
  {   
for(j=0;j<8;j++)
  {
stars[i][j+1] + stars[i+1][j] + stars[i-1][j] + stars[i][j-1];}}

instead of writing many lines of this type of code:

stars[1][1] = stars[1][1] + stars[0][1] + stars[2][1] + stars[1][0] + stars[1][2]

Yes, the loops are better. Just format them correctly.

Also, I'm reading in the array from a text file with all integers, the array is declared and initialized as 0.0. When I add the numbers in the four surrounding spaces for one position as a test, I get 33 which is the correct answer. When I try to divide that number by 5.0 to get an integer, I get 28.2 instead of 6.6. I tried doing

(float)stars[i][j]

after reading in the int values from the file, but to no avail.

Any suggestions on that?

Yes. Give us the information we need to understand the problem. Code, values, etc...

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

If the file was included just open the file. If the file was not included, open stdin instead.

if (filename_entered)
    open file
  else
    open stdin
end if
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

Use srand()

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.

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

That's another way of saying the same thing. :)

No really. You said "It's all about the interpretation of the bytes when loaded into memory that matters, not how they're stored." I'm saying you must know how they are stored or you can't properly read the data.

I think I know what you were trying to say, but it wasn't clear.

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

So don't reply again. Edit your post. It's quite simple and proper.

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

and I regard all of that with great respect.but honestly thats life and it wont change.let me put it this way
you're on youre way to buy a expensive sofware[witch they always are] and your geeky friend comes over and offer you a cracked version with no prob's to experience, are you going to reject him and go pay x amount?dont think so

Then you don't understand most of us. There are many people here with integrity.

I do understand you and feel for you.aunistly I usually use crack stuff

I'm sorry for you, then. No wonder the price of software is so high -- to make up for all the people stealing it. Thank you so much...

And, by the way, in English, there is always a SPACE after the period ending a sentence. English is not a web address.

sittas87 commented: I was a little silly.my apologies +2
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

- How does endian-ness work? If two bytes A and B are written ( in that order ) into a file using put, then read on a machine with reversed endian-ness, do the bytes come out in a different order ( i.e. B, A ), or do they come out in the order A, B, but with the actual bits themselves being backwards?

It's all about the interpretation of the bytes when loaded into memory that matters, not how they're stored. If the first machine writes A,B and the second machine reads A,B, the values will be interpreted differently because to the first machine A,B means A,B, but to the second machine A,B means B,A.

No, it's about how they are stored. The bytes will be backwards in the file when written by a big-endian system but read by a little-endian system.

Matt, see this

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

The google search you posted did not have anything related to programming.

It actually did answer the question AD thought you asked. Which was more than I could do because to me your question doesn't make sense. What does the system() command have to do with the size of the command prompt?

Do you think I'm an idiot?

Not necessarily. But when 95% of the new posters here are, you unfortunately fell into that category. But then, if you rephrase your question so we understand what you are looking for, all doubts will be removed.

PS: Are you, maybe, talking about the command window -- not the prompt?

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

Since comments cannot be nested, a stack really is not necessary. A simple flag would suffice. Although a stack would be really helpful if you are looking for braces/parens/etc, things that can be nested.

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

Look into the time functions in timer.h for ways to do the counters. If you need help in other areas, please be specific.

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

Lives in Philippines -- "be more pacific"! Ha Ha, that's funny!!!:)

iamthwee commented: i agree +16
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

hmmmm but why would we expect anything else but integers between 1 and 100?

Thats not really the point ^_^ But its always good practice to make sure that there are no warnings what so ever.

williamhemsworth is correct. And in addition, haven't you ever heard of extra credit, or maybe mistakes?

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

IMAO, a restructure of the data file should be done. Don't rely on a number to tell you that you're done with the tracks. I know many songs that start with a number:
76 Trombones
8675309

for example.

Add to each line a type value that tells you what the line contains. Each line should have a value for ease of programming. Also, if some information is not available, it won't mess up the program.

1,1234 //this is id number
2,Scarlets Walk //album name
3,Tori Amos //artist
4,24 //price
5,2 //stock
6,Pop //genre
7,Track A //track name
8,10 30 //track length in mins and secs
7,Track B
8,5 0
7,Track C
8,3 15
1,1254 //this is id number
etc.

Now, when you don't read a 7 or 8 line you know you're done with the tracks.

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

my point is that STRSTR function will do what the OP wants

i thought it would be readily apparent to anyone who takes a moment to look it up. sorry, if i don't type out a fully commented and validated solution.

Not necessarily. Without knowing what needs to be done, looking up a function generally is not of much help. After your explanation, your solution is readily apparent.

I'm not really criticizing your recommendation, just that how you meant to use it is a good thing to add.

jephthah commented: good point. +4
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

And few are willing to try to understand 167 lines of uncommented code not knowing what they are looking for. Try some explanation and detail so we don't have to waste our time.

And if iamthwee is right about point 1 (too bad he didn't link to proof)...