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

I have having trouble with this problem.

That happens a lot.

Can you guys advise?

Sure. Write the code correctly.

I am not sure I am doing this right but this is what I have now.

Neither do we, since we didn't write it. It looks like it's doing exactly what you told it to.

I searched around there was one using while loop.

And this is important how?

I am not sure if its necessary.

It might not be.

I am using Borland btw. Thanks in advance

Sure. Any time.

If you need more help, maybe you can tell us what the problem is so we have a clue what to look for. Only a couple of us are psychic.

Salem commented: "Only a couple of us are psychic" - I knew you were going to say that ;) +19
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

above programme is right,

but you have to maintain the indentation in the programme.

After you posted that unformatted code in another thread you have the gall to mention indentation here? Practice what you preach.

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

You can ignore that code also. Be sure you write your own code micjan and don't even bother with code that has no formatting, improper function definitions, bad headers, etc.

And to rajendra.parmar, skwatamkar, and abc@ -- please refrain from doing other people's homework for them. They learn nothing, and you are posting code that will probably not get an A because of the above.

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

Help you with what? You didn't give us a clue what kind of help you need.

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

Or you could try reading the Member Rules, or the pertinent sticky posts at the top of the forum, like the post Read This Before Posting

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

Would you like to help pinpoint where the problem is a little closer than somewhere in 200 lines of code?

And have you ever heard of periods? They are those little dots that end sentences. They are used to help others understand the written word so they don't have to decipher run-on sentences.

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

Do you understand the difference between inFile >> mazeStore[i][j]; and inFile.getline(ch,12); :icon_question:
Is one a perfect replacement for the other? In other words, do they do exactly the same thing? If not, why did you simply replace one with the other?

I am a huge believer the that stuff between the ears needs to be used. If all you're going to do is blindly try any thing in the hopes you'll hit on the magic code, forget it. You need to use your brain to program. What does the statement do? How is it best used? How does using something else change the code needed around it?

Original code:

for (int i = 0; i < 12; i++)
         {
            for (int j = 0; j < 12; j++)
            {
               inFile >> mazeStore[i][j];         
            }
         }

This uses 2 loops to read a single mazeStore entry 144 times -- whatever mazeStore is.

You change it to read an entire line:

for (int i = 0; i < 12; i++)
         {
            for (int j = 0; j < 12; j++)
            {
               inFile.getline(ch,12);
               ch[0] = mazeStore[i][j];         
            }
         }

which reads a line 144 times. And after each read moves one unknown character into ch[0] -- which is the line just read.

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

Reread my post.

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

You aren't retrieving the value in main() .

Where does calcDays (numberOfDays); put the value upon return? Do you need to brush up on returning values in you book?

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

But I'm lost on how to use it

Look it up in a reference book, a tutorial, google, whatever. If you want to use something, you need to find out how. That's extremely simple.

and where to put it at.

What did I suggest? Do I need to be clearer?

I've tried messing around with it. But every time I use it the program doesn't run correctly. :sad:

We have no idea what you tried, so we can't suggest a correction. No info, no fix.

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

I don't see where you even tried Fbody's suggestion

cout<<"Even numbers are:";
for (int i = 0; i < numberOfEvens; i++) {
  cout << " " << y.even[i];
}

All you are doing is testing i with a value in the array. That's never going to work.

How many values were put in the array? Keep track. Then use that value in the loop.

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

Ok, it looks like someone needs some major handholding. Either that or you can't be bothered to think and want us to do it for you. I'm hoping that's not the case.

From 4th grade math (I think):
How do you know 2 is a factor of 12? What is it that makes it a factor? How did you arrive at that answer?

Please explain in detail. And "because 6*2 is 12" is not an answer.

Same questions with 3, then 5.


And considering you can't do something as easy a finding the factors, where did you steal the prime number code from. There is no way you wrote it.

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

Yes it is completely possible. First, stop flooding the forum with useless messages. When someone logs in that can answer the question, they will answer. No need to badger us when no one is logged on. Second, use sentences. Don't run all your sentences together. In order for us to understand you, you need to post in an understandable form. This is not a chatroom.

You will need to look through MSDN.com. You can start here

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

As the code stands, I see nothing wrong with it. It does exactly what you told it to -- tells you if each of two values are in the proper order, and which values.

If that's not what you want, you need to tell us more than "what's wrong with this code". You need to tell us what you wanted, what the code is doing, and what the code should have done. Details. Examples.

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

But DaniWeb didn't exist in the 80's. Who would you hassle without us?
:icon_wink:

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

Because arr is not a pointer. It is the address of the array and you cannot change to it.

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

Or look at each character of the string and when you find a space, convert the characters you just passed over.

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

Hello All,

I seem to be having a relatively small problem with a Maze program I'm working on. Basically, I read a txt file containing a maze into a 12 x 12 2D array, but the problem is when I try to display it, none of the white spaces that are in the maze show up. I know it has something to do with the getline() function, but I have no clue as to where to put it.!

What getline() function? You have no getline() function.

Or do you mean you want to use it but don't.... OK, I guess that makes sense.

Let's see, you have input which doesn't work. You think getline() will make it work. How about where you do the input right now? That would probably be a good place in my opinion.

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

Of course it will help you a lot. You won't have to do it yourself. But that's not how the Forums work. Remember the Rules you read when you signed up?

1) Start by programming 1 simple piece of the project -- Menu and Input. When you have the menu and input tested and working...
2) Add a function to add the input. When you have Add tested and working...
3) Add a function to subtract the input....
Go right down the line adding and testing all the functions. You'll have part one done in no time.

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

Basically I have a 32-bit UNSIGNED register. I want to know how to calculate the value when I subtract 1 when the value is zero (all zero bits).

So you have 32 bits of 0. Assume for argument's sake there is a 1 in the next bit (bit 33). Subtract 1. What do you get?

That's how you figure it out.

The easy way is to simply know that if you have all 0s, when you subtract 1 you get all 1s. Doesn't matter if the value is signed or unsigned.

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

12&1
6&2
4&3

why?

Because now you can code the program. As thomas implied,
1) What are the steps you used to figure that out?
2) Write those steps in detail
3) There is the beginning of your code.

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

the actual code would be helpful not just the errors.

Why? "cannot open shared object file: No such file or directory
" has nothing to do with the code.

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

You really need to be clear and explain in detail what you need.

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

Note I used an if . Can you tell why you can't just do the replacement without the if ?

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

The array is not defined in the function definition correctly.
There is no way the compiler knows how big the array is using void descend(int arrayx[sizex][sizey], int sizex, int sizey) sizey in the array definition needs to be an explicit number, not a variable.

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

What are the factors of 12?

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

Sit down with pencil and paper and execute the code a line at a time. Watch what values come up. That's the best way to understand anything.

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

With what? You haven't shown us anything we can help you with. Based on the info you've given, all we can do is write it for you.

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

That seems reasonable, but what if the buffer is 20 characters and the user only enters 4 and hits enter?

What would the value of strlen() return in that case?

jonsca commented: Yup +2
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What seems to be wrong? It really helps to explain the problem.

And if you made changes, you need to post the new code. You might also want to post just the part that has the problem.

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

Actually, 13! is larger than the maximum size your variable can hold. That's why it doesn't work. Nothing to do with ASCII 13.

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

Neither lol.

int i;
for (i = 0;array[i] !='\0';i++)
{ 
    if(array[i] =='\n')
       array[i] = '\0';
}

Or maybe:

l = strlen(array)-1
if (array[l] == '\n') array[l] == '\0';

Saves a for loop.

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

First thanks to both of you for your help.

Sure. Did you figure out what was wrong with 13? What was it?

Second i did the code by my own and it works somehow.
It´s not like i was asking for everything :-P
I just need little help by these 2 parts. I guess this will be okay by the teacher.

I was not suggesting you wanted someone to write your code. What I was saying is that by using someone else's code that you don't understand in your code, your instructor may believe someone else wrote it all. Keep your code at a level you understand.

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

Moved...

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

Oh yeah...I was going to say that, too. Don't use goto.

And, you can't actually control what the user will input. But your program can handle it if they do not enter what you want them to, if you add the code I suggested. :)

And he can fail the project for using code that was not taught and has no business using. The instructor will know that the code was given to him and he's getting others to write his code for him.

Please keep in mind who is asking and their level of knowledge before showing your prowess as a programmer and blowing their mind with higher level techniques. :icon_wink:

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

So how exactly would you implement that?

By writing out a string and doing what I recommended on paper. Then understanding what you just did and writing code based on that understanding.

Changing all the alphanumeric characters to a new string?

Changing? Did I say changing?

And this is to test for palindromes, so I would just do it twice for the forward string and reverse string right?

Again, think about how you can tell a string is a palindrome. Write it out.

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

1) Are you certain the program is handling 13! correctly?

2) Not easily. Do you understand how to parse strings? If so, you can try it. If not, you might want to wait a while to handle that.

3) Do not use goto . It is considered bad programming practice since a loop can easily be used instead and to better effect.

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

Yes it can.

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

I think you can figure it out if you put your thinking cap on...

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

Copy you string a character at a time to another string. But move only the alphabetic characters. See firstPerson's link

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

It ignores preceding whitespace. But If you don't remove the '1', buffer never changes so you keep reading it.

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

Here's what I see.

sscanf(buffer, "%s", CPU_burst_temp);  // what is the value of buffer here?
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);  // what is the value of buffer here?
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);  // what is the value of buffer here?
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);  // etc...
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);

What is changing buffer?

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

Excellent. You now know you have to read everything (including your book) carefully.

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

Hey everyone, I have a simple simple problem that is literally driving me insane.

Basically, I have a database of numbers seperated my spaces. I just took a segment of my code because I am soooo completely lost with the output!!

If the database is:

1 2 3 9 7 6 4 5 6 7

I don't understand why my output is 1 for every single line??? How is the sscanf statement not taking the next string??

Someone please help I am desperate!!

You have a data base or a data file?

It's obvious that your code doesn't run.

fp = fopen("CPULoad.dat", "r");
	fgets(buffer, 20000, fp);	
	
	fp = fopen("CPULoad.dat", "r");
	fgets(buffer, 20000, fp);

You cannot open the same file twice, so you never get to any output at all.

Please either describe the real problem or post the real code.

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

I repeat:

And be sure we can tell what line the compiler is complaining about.

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

Okay, here's the code, reformatted, for just the part that's giving me trouble.
Before, the code fine in my text editor, but the tabbing / spacing was changing when I pasted it in here.

MUCH better. Be sure to read the link I posted. It will help you correct your TAB problem.

Like I said, I'm very new to C, so there could be lots of errors that I'm making in here, not necessarily related to what I'm asking about, so don't hesitate to correct me :)

Oh oh, free hand given. You are in real trouble now! :icon_wink:

char input;
char username[50],password[50],type[50];
printf("\nEnter a new username to add:");
fgets(username,50,stdin);    // Don't forget that FGETS keeps the \n entered.  You probably have to get rid of it before using STRCMP
printf("\nEnter a password:");
fgets(password,50,stdin);
printf("\nEnter a type:");
fgets(type,50,stdin);
int k, compare;        // This is C.  You need to create variables at the *top* of the code block, before executable statements.  Move this line up with the CHAR line.

And never tell us the error you get. Post the error exactly as the compiler shows it. And be sure we can tell what line the compiler is complaining about.

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

No you haven't.
An input statement is scanf() . It puts a value into a variable entered by the user.
I said "Change the input statement into calls to rand() ", so if scanf() is an input statement, and you continue using it, you are using an input statement.

Use rand() to put a value into the variable. And I'm certain your source never showed you how to use it in a scanf() statement and not only in a printf() statement.

Search this forum if your source is still confusing you.

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

Alright, here's the full code (it's still hard to read, the formatting is getting a little messed up when I post it here)

Then read this.

And didn't I say it's OK to post only the code with the problem? You could just reformat that part so it can be understood.

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

Change the input statement into calls to rand() .

Did you actually change your input statements in to calls to rand() ?

Be sure you look up how to use it correctly.

Did you actually look up how to use rand() ?

It looks like you completely ignored both my suggestions and did your own thing.