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

time() returns a type time_t srand() wants an unsigned int
Look up casting in your book.

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

Why do you first time posters always leave out the most important information to understanding the problem? The response to the information given:

I wrote the code but its gives me error

That's because you did something wrong.

Don't you think what the error is might be an important piece of information?

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

#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <iostream>

This won't work at all using most compilers. I dare you to try it on VC++. Using O/S and compiler dependent solutions are problematic. These include
* system("pause"); * Any Linux/Unix-only solutions: unistd.h and termios.h * Non-standard C function getch() , defined only in particular compilers on one O/S


The BEST answer is the original post in this thread. Please learn and understand why. Don't just post your favorite inadequate solution as the best way. In these forums we try to teach the proper way to do things, not the hackneyed way most people are taught by less-than-adequate programmers.

More here

Flaming another contributors post is definitely not the way to go about gaining a great reputation here.

There was no flame. The suggestion of system("pause"); is shitty. Nothing against you, just that suggestion.

cout + cin.get() is all you need in C++
printf() + getchar() is all you need in C

printf() is a huge hit on resources. If all you are doing it printing a character or a string, use putchar() and puts() repectively. :icon_wink:

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

Open the data file as #1
Change the LPRINT statements to PRINT#1,
When done CLOSE #1

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

Wow! That's a lot of code!

I agree. Over 1000 lines of code with no indication of where the problem could be. That's why I for one ignored the question. I already have a full time job and don't need another one being a code maintenance technician -- unpaid no less.

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

Try changing your output from cout << w1 << " " << setw(8); to cout << setw(8) << w1 << " ";

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

Also
1) Improper C syntax

int loadToFile(FILE *f,int *file_pointer,int *length){
    *length++;             // executable statement
    double size=6000000;   // definition statements
    int i;
    char *substr[6000000-50+1];
    unsigned int no_of_strings;

You must define your variables before executing any code

2) You may be trying to create too much data. 78M on each call with no indication of freeing up the malloced buffers.

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

Since you posted this as a POLL, not a request for help, my poll response is:

You may fail your course because you cannot follow instructions. Following instructions is a requirement in programming. See the Member Rules as requested when you joined.

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

"For Writing" - If the file does not exist, create it. If it does exist, open and empty it.
"For Update" - Open an existing file, you can read from it, you an also write to it.

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

Do you really need help counting the characters in the string? Look at the methods associated with a string object.

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

If you need help, you need to explain what you need help with. All you've said is "Here's what I need to do, Here's my code." No mention of what the trouble is.

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

To all those here asking for us to give them free code --

That's not how these forums work.

You do the coding. You post what you have that doesn't work. We help you fix that code. Do not post code that simply has a comment that says "// this is where I need help"

jonsca commented: Make this post a sticky! +5
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You have to look at your input file and figure out how to read each field. Since you know how to open a file, look up how to read the file. It must be within the next couple pages of your book.

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

please can anyone make a program in c language to convert a number from decimal to hexadecimal system using switch statements and please don't use arrays or %x

Yes, many of us can. But since we aren't a free coding service, we won't. But we can HELP you do it. You just have to start and post a real attempt that we can help with.

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

Can anyone come up with a loop to read the the data from the file and read it into the dat?

Yes we can. But it's your task, not ours. You come up with the loop and if it doesn't work, we can help you fix it. But we aren't going to write it for you.

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

As I said, a char is just another number that outputs as a letter.

char xchar;
cout << "Type a character:" << endl;
cin >> xchar;
xchar = xchar + 3;
cout << "The character is now " << xchar << endl;
cout << "It's new value is " << int(xchar) << endl;  // display the number value
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I keep getting location errors c4700 errors and a bunch of initializing errors
Here is the full code again I really do not know how to fix this.

Neither do we, since we don't know what initialization errors there are, nor where. And c4700 doesn't mean anything to us non-compiler humans. It helps to give us ALL the information you can so we can understand the problem.

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

Like we said in your other post, you need to decide if you want to write a C program or a C++ program.

#include <iostream>     // C++
#include <conio.h>      // non-standard- don't use
#include <stdio.h>      // C
#include <string.h>     // C

int delete= 0;


char parname[20];
char valname[20];
FILE *fdin;             // C
char file[300];
char infile[60];


void main(int argc, char* argv[])  // neither -- main is an INT
{if (argc!=3)
return;
strcpy(parname,argv[1]);                         // C
printf("Enter Parameter Name= %s \n",parname);   // C
printf("Enter the value= %s \n",valname);        // C
if ((fdin=fopen(infile,"r"))==NULL)              // C
{ printf ("Open Error");                         // C
return;
}
for (n=0 ; fgets(file,sizeof(file),fdin); ++n)   // C

for (i=2;i<argc;++i)
{ if(strcmp(argv[i],"-D")==0)                    // C
delete=-1;

You've only got one statement (unused) that is C++. Try rewriting it using C++ commands ( cout , string s, etc) and we can help you much better. Or use C and continue with your other thread.

Also, please format your code so everyone -- including you -- can follow it as it gets bigger.

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

You can't. graphics.h is not compatible with Dev-C++

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

Microsoft says what I wrote above: http://msdn.microsoft.com/en-us/library/29dh1w7z%28v=vs.80%29.aspx

Since M$ is only concerned with M$, what they say is perfectly valid -- for the M$ world. But they are not the only game in town, and these forums cater to all games, not just the M$ world. Hence, portable has a different meaning.

IOW, portable does not mean "Works with Microsoft" here.

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

Simply add one.

A char is simply a number output in a special way. B is the value 66, C is 67.

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

Around here, portable means works on all compliant compilers of the Language Standard.

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

When you want your program to be portable

Excuse me? Don't you mean not portable?

You wouldn't use them. IMO, they are unnecessary.

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

By writing a program that creates that output.

Did you bother to read the Member Rules as requested?

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

I use c++ if u could provide some help i would appreciate thank you :)

Then why are you posting in the C forum? :icon_rolleyes:

We can't help you with C++ in the C forum.

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

That's because you didn't learn C++. You learned C. And if your instructor was really teaching C++ then you unfortunately received a very bad education. It's like taking a car repair class and all you worked on were motorcycles. After all, they have engines, too, don't they?

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

Did you ever consider that you equation might contain operands larger than 9? How do you store 35 in one character? Methinks you need to rethink something.

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

I didn't understand why for(...) at the beginning, a has to be 0.

Who says it has to be 0? Test it. First thing in the program just print out the array.

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

You are doing too much at one time. Take the program a setp at a time so you can understand completely what you are doing.

First, read the file and output what is read. No more. Then

1. count number of words in file

Get that running completely (and properly) before moving on. When finished, add

2. print all words, one per line

Get that running completely (and properly) before moving on.

What you are doing is trying to get everything done at once which makes it hard to really understand the steps -- the forest/trees problem. It also makes debugging and error checking much more manageable. And with only one step at a time, you won't have the problem that

there are many errors but I dont know how to fix it.

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

You are passing a single character. atoi() requires a character array (pointer)

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

Two floating or double values are rarely equal. You have to test for a range -- maybe within 0.001 tolerance.

if ABS(val1 - val2) > 0.001)  // check if close to equal
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
const int DAYS = 7;
const int TEMPS = 2;

for (int days = 0; days < 7; days ++)
{
    cout << "Enter High Temp for day " 
         <<  days + 1 << ": ";
    cin >> xTemps[DAYS][TEMPS];    // This always loads xTemps[7][2] --
                                   // which does not exist.  Max is 
                                   // xTemps[6][1]

    cout << "Enter Low Temp for day "
         <<  days + 1 << ": ";
    cin >> xTemps[DAYS][TEMPS];    // Overwrites the above.
		  //end for
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

//Why did the compiler treat it as a function...

Because it IS a function.

... and are the header files lacking or what?

No, you are lacking the header file.

But why do you want to use a non-standard C function when a standard C++ function works just fine? Use cin.get() Also, main() is an int function, not a void function. See this

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

What you're asking doesn't make sense.

I'm aware you can use putchar() but that doesn't really work so well for what I'm doing,

Why? Are you trying to output a character and it's outputting a flower? Maybe you need to be more explicit so we can understand what you're doing.

or at least I'd like to find a quicker way if there is one.

Huh? What's quicker than putchar() ?

Is there not a tochar or basically the opposite of toascii?

No, a function like tochar() is worthless.

A char and it's ascii value are identical. It's just two different names for the same numerical value. Like hot dog and wiener.

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

You already check to see if the word has 3-6 characters. Also check for an ' too before inserting the word.

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

Who's teaching these people computer lingo????

My program runs a three dimensional maze (3x3x3 = 27 locations)...

No, it creates a maze. It doesn't run anything.

... the start and exit rooms show up on the run compiled program ...

A "run compiled program?" What the heck does that mean? Run from the IDE?

... but the code room does not cout?

Things don't cout. Things get displayed. A ? is used after a question, not a statement.

However if run from the executable created it shows all three rooms, re-running of the compiled...

The executable created is the compiled...

It compiles fine apart from this ...

It compiles fine because you have no syntax errors. Logic errors do not affect compiling in any way.

... and I am not sure if somehow I've coded it wrong, although then it should have thrown up compile errors?

If it doesn't do what you wanted, you coded it wrong. If you write your code wrong, how is the compiler supposed to know that? If your code is cout << "Yellow Woild" << endl; , do you expect a compiler error because you misspelled Hello World?

do {
   time_t now;
   time(&now);
   srand(now);

srand() and all this time stuff should be at the top of the program, not in the loop. All you are doing here is resetting the random generator to the same value, which generates the same random values each time the loop runs. …

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

Then you did it wrong. Are you really asking us to write it for you? We can't help you fix it if we can't see what you did wrong.

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

Would a statement like

When posting programming code, encase it in [code], [code=syntax], where 'syntax' is any language found within our Code Snippets section, or [icode], for inline code...

be what you're looking for? This is at the top of the C and C++ forums at least.


How about

Use code tags!

There are two ways to wrap code in code tags. The easiest way to do this is to select all of your code and then click the # button on the message editor. This will automatically wrap the selected text in code tags. Without code tags, all leading whitespace will be removed from the code, so nicely formatted code ends up having no indentation. This makes the code very hard to read and many members will refuse to help you because they can't read your code.

This is in the sticky post Read This Before Posting. Yes, it needs to be edited to reflect [CODE] instead of #, but this has been available also for a long time.


The Member Rules (link at the top of every page) state

Keep it Clear
* Do wrap your programming code blocks within [code] ...
[/code] tags

On the background of the text box where the questions asked are typed in is:

Please wrap code in [code] and [icode] tags
Here is php code: [code=php]echo ":Hello World";
[/code]
Here is c code: [code=c]printf("Foo\n");
[/code]
Here is …

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

Yes. But why end all cases with the same 3 statements? Put them once at the bottom of the loop after the switch structure.

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

First, after entering the number, calculate the number of days.
With what's left after removing the number of days from the number, calculate the number of hours.
Keep going until you get the seconds.

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

Yes, we can help you.
Yes, it is a silly request since you didn't bother to give us any indication what you need help with. We don't code for people, we help you fix your code.

Be sure to read the Member Rules.

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

You don't. The C++ standard does not allow for "hit any key" type input. You need to change your prompt to "Press ENTER to continue"

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

You can't use .eof() in this manner. You don't hit EOF until you actually try to read past end, not when you read the last character. Therefore, you will go through your loop the final time after reading bogus data.

Read the first characters before the loop. Then read the next characters at the end of the loop.

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

Use toupper() on LETTER. That's your input.

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

You never load result with anything. At the end of inttobin() you've loaded buffer, then output result. But you didn't move buffer into result.

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

So turn your comments into code. We aren't a coding service, we help you fix your code that's wrong. Your code isn't wrong, it's non-existant.

Also, explain the problem you're having. All you say is your problem is with random numbers. I see nothing about random numbers in your code, just in comments.

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

Ok so the program compiles fine, but when I actually run it, it is not printing out the result of intobin(hextoint(st))

So finish formatting your code properly and post it. We can't do anything if we can't see the current code. And please explain in detail what's wrong, not just it won't print.

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

I prefer to have menu do the menu only. No loops, no decisions. It returns the selection to the calling function where it is analyzed and acted upon.

Each function should do what the name implies, not a bunch of additional work.

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

Two things:
This is a C forum. Why are you including

#include<windows.h> -- this should not be necessary
#include<conio.h> -- this is non-standard and should not be necessary
#include<iostream.h> -- this is strictly C++

Learn to consistantly format your code. Here's some information to help. If code is hard for us to follow (which yours is), many will simply ignore your code.

Error 1: In C, all variables must be defined at the top of a function, not when you decide to use them.

Error 2: obvious

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

For the third part of the question, set the 3rd array to all zeros. Then you can simply use the element in the first array num[i] to increment the proper element in the 3rd array: dist[num[i]] In other words, if first array contains:
num[]= 2 4 3 8 5 3 4 2 8 4

the 3rd array will contain
ctr[0]=0
ctr[1]=0
ctr[2]=2
ctr[3]=2
ctr[4]=3
ctr[5]=1
ctr[6]=0
ctr[7]=0
ctr[8]=2
ctr[9]=0

You don't need the double loop.

The answer to the second question can now be loaded into dist[] just by looping through cnt[] and looking for != 0