~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

IMAO,...... *a good beating*

Yeah yeah yeah....I really get to see a lot of IMAO nowadays....;)

Anyway, if you notice the times, I was responding while she was posting. It takes me more than 60 seconds to type up a response.

Her post was at 1.30 AM and your answer was at 1.22 PM :twisted:( at my place but still 12 hr difference no matter where you stay)
Maybe Miss Dani should start using a 24 hour clock....:P

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

First thing, your function definition should be void deleteS (char* ptr) -- this passes in a pointer, you are passing in an array of pointers.

That was a typo I think, she has already taken care of this in her last post.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Both counter and ptr point to the same memory space.. so when you modify ptr in the loop you end up modifying what counter points to.

And btw have you read the description of strcat( ) since it is not doing things in your case whch you are expecting it to do.

You can look into memmove to solve your problem.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Maybe this should help:
LINK
ANOTHER ONE

Just let me know if it is any good.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

For multiplication:

a[i] = ( a[i] - '0' ) * 2 + '0' ;

But this would work only for digits from 1 to 4.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Well exactly. I didn't put a in my code, I just put a.

Even I know that, I was just clarifying Mr. Andors point.

I don't think I can use isdigit because I'm not supposed to decremenet 0.

What does this mean ?

How is '0' - 1 = 9 ?!

If you encounter 0 in your string, how would you handle it.. surely you cant put -1 ?

And what if my task was to, say, multiply every digit found by two? How would I do that?

But suppose your digit is greater than 4.. then how do you propose to do it since it would then imply making spaces for new digits ?

State your program requirements in more detail.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

First of all dont use scanf() to obtain the string from user, use [search]fgets( )[/search] which is much safer option. Something like:

char buffer[BUFSIZ] = { '\0' } ;
printf( "Enter the string: " ) ;
fgets( buffer, BUFSIZ, stdin ) ;

Also there is an inbuilt function in the header file ctype.h known as [search]isdigit( ) [/search] which returns a non zero value if the given character is a digit character.

In your while loop check for digit character, increment the counter, and scale the result in the range 0 - 9 after subtracting the value with 1.

eg.
'0' - 1 = 9

And I don't quite understand how you can use 'a-=1' when a is a string

a is a character array but a is a character and you can add or subtract from a charcter since they are basically represented as integers ASCII values.

If any doubts repost.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Naa here I didnt write the code buddy, just replaced the one function call with another thats it.

Also the MOD rules state that I am not allowed to post entire code to someone so I wouldnt dare go against them.

Oh.. wait.. damn..my crown jewels are dropping down..hehe

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Not wrong though, since I corrected my mistake before anyone else did. Plus I don't have a compiler to test anything.

Agreed...

If you've got a free basement I've got some chicken wire.

Its a good thing that I am non English plus I dont get to understand 99% of your humour, so all is well. :twisted:

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

In hindsight I think that might be ok.

Hehehe look before you leap. ;)

Here is the workiing code. I just replaced fscanf( ) with fgets( ) and it was fine.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define numofstr 50
#define strleng 101

int compare(const void *,const void *);

int main()
{
    FILE *a,*b;
    char s[100] = { '\0' } ;                            /*starr - string array*/
    int i=0;
    char starr[numofstr][strleng] = { '\0' } ;

    a=fopen("non_sorted.txt","r");

    while( 1 )
    {
        if (! fgets( s, 100, a ) )
            break ;

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

        strcpy(starr[i], s) ;
        i++;
    }

    qsort(starr,numofstr,strleng,compare);

    b=fopen("sorted.txt","w");

    for(i=0;i<numofstr;i++)
        fprintf(b,"%s\n",starr[i]);

    return 0 ;
}

int compare( const void* a, const void* b )
{
   char* arg1 = (char*) a;
   char* arg2 = (char*) b;
   return strcmp( arg1, arg2 ) ;
 }
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Nope. Still doesn't work.

Ok paste your entire code along with your unsorted data.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

I'm helping by offering him good advice. You on the other hand have given him a piece of code that don't work. And you're leading him down the garden path.

If you don't like the truth then I'm sorrie.

Yeah I like truth but not twisted truth.

I gave him google links of how qsort is applied. I didnt write that tutorial so any problems he is facing I am trying to help him out unlike you who only says this is wrong and that is wrong without giving a shred of help.

Actions speak louder than words. All you do is just say this is wrong and that is wrong but never attempt to help someone out.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

fscanf( ) accpets the addr of the variable where the data is to be stored.

What you have is:

while(fscanf(a,"%s",&s)!=EOF)

Try replacing &s by &s[0] or s and then see.

No offence but I'd scrap that and try again. SOS has given you a crappy example ha ha.

If you cant help atleast keep your bad sense of humour to yourself.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Hmm looks like the code sample is broken.

Try this:

int compare_ints( const void* a, const void* b ) {
   char* arg1 = (char*) a;
   char* arg2 = (char*) b;
   return strcmp( arg1, arg2 ) ;
 }
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Study the links which I have given you, my writing the code wont help you in any way.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Dont use direct assignment operator. You can't treat strings like normal data types in terms of assignment.

You need to use strcpy to do somthing like that.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

For a solved eg. see here.

For documentation look here.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

P.S. I'm asking because I want to arange my list of music albums, because right now, the names are held in a completely sporadic manner in a Word document.:cheesy:

Copy and paste the contents of the word file in a clean text file to avoid unnecessary hassles. I dont think there would be a requirement of your course to read specificallly from a *word file* .

Word File is not a text editor but used to present text and such tools are not used for storing data. Data files of user either have a header format which the user has designated (like a game's save file) and he knows or they are just pure text with custom extension (names.sdc) .

This might sound stupid...but what is a container? Either it's an English word for something I'm familiar with (English isn't my first language) or I simply don't know what it is.
And please name some containers.

Simply put a container is something which is used to store loads of data or records and which can be accssed in O (1) time if the index of that element is know.

THe basic containers are well -- arrays :D in C and Vectors in C++.
If you using arrays you can use the inbuilt qsort( ) method to sort the array and if you usign vectors you can use the inbuilt sort( ) algorithm.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Maybe you should read this regarding your size query. It is an excellent example and maybe will suit your needs.

Also regardign your pointer query read this.

Hope it sufficed bye.

The three have a size of 4Bytes, no matter what type it is. But, how to find out the restrictions?

Pointer of type void can point to any type of data if thats what you asking while int type pointers point only to int data and char type pointers point to char type data.

Previous versions of malloc returned char* so we had to typecast them to suit the needs of the program but it has changed in C99 where malloc returns void pointer (void* )

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

I have read this somewhere in the context of Processor architecture. The compiler tries to align the data and the pointers along the boundaries i.e. in multiples of 4.

So the addresses are given along the lines of multiples of 4.

Not very sure about it but atleast this should give you a starting point.

JoBe commented: Great help, nice person. +3
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

1st, IMAO you do not have to post the entire program, just the piece you are trying to fix, as you did.

thanks, I didn't see the point in posting the entire code either but I was trying to be cooperative,

Hmm....

Anyways gettign back to the topic, when you say it doesnt work, why dont you tell us exactly what kind of output are you expecting, what output are you currently getting, what kind of sample data are you supplying to your program. The function on the surface looks fine... just a random thought, try replacing the != 0 with > 0 in the while loop.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Actually, it's feof() that's the problem. EOF is a good thing to use, it just has to be used with a function that returns it... which fgets() does not...

Yeah looks like I am desperate need of eyeglasses.

Now back to topic::

Please post your entire code when you update your code.
BTW where have you initialized "fname" variable.

Also if you want to read in entire line from a file, you need to use fgets( ) .

int longest(void)

{
    char line[BUFSIZE] = {'\0'} ;
    int counter=0;
    int longest=0;
    char fname[128];
    FILE *file;
    
    file=fopen( fname,"r" );
    while (fgets( line, BUFSIZE, file )!=0)
    {
// after reading using fgets( ) you need to remove the '\n' at the 
// end of the string or subtract 1 from the strlen( ) result since it will
// always include a '\n'
      counter=strlen(line);
      if(counter>longest)
      {
         longest=counter;
       }
    }
    return longest; 
}
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

As far as why not to use EOF is concerned, simple it reads the data from the file one too many times. For detailed explanation, see an excellent tuts by Mr.WaltP and if possible read all of them , they are good.

And btw, where are you opening the file which is passed to the "longest( ) " function. If you are opening it somewhere else then post the entire code.

If you are not opening the "file" stream, then thats what is causing the crash. Before using the file stream you have to open it like:

FILE* fp = NULL ;
fp = fopen( "hello.txt", "r" ) ;
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

If you callign the correct( ) function in main( ) which has the variable "round" you can do something like:

void correct( int i, int round )
{
   // do whatever you want 
}

// In main
correct( i, round ) ;

Though I dont know what you are tryign to achieve here( i havent followed the thread closely) maybe the above thing shoud be helpful to you..

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

EIther you can put this code in main itself or can pass the variable "round" to the function. It says undefined since the variable "round" is out of scope of the function.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Hehe funny...

:mrgreen:

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

what is this MR-green stuff?
what does it mean

Huh.. what you talking about ?
Who / where is Mr. Green ?

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Just declare an array of characters. I think 512 should suffice for your purpose.

char buffer[BUFSIZ] = {'\0'} ;
fgets( buffer, BUFSIZ, stdin ) ;
// here BUFSIZ is a macro which is defined as 512
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Because EOF is actually a maro which is defined in stdio.h as #define [B]EOF[/B] [I]<integer constant expression < 0>[/I]

The macro yields the return value used to signal the end of a stream or to report an error condition

In your case its the end of stream thing which is causing the break of loop.
In Mr. Dragons case it is the 0 which is returned by the fgets( ) which is causing the termination.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Okay here are a few pointers:

1. Use int main( ) not just main( ) and append a return 0 stmt at the end of main( ).

2. Dont hard code the file names in the function, its not a good practice. Just keep the file name in a constant array of characters.

3. I am not sure on this one, but, dont use \\ in your file paths, it is not cross platform complaint. Use forward slashes instead ( / ).

4. After opening the files do check whether the operation is successful, it is a good practice.

FILE* fp ;
if( ( fp = fopen( "a.txt", "r" ) ) == NULL )
{
     fputs( "file opening failed", stderr ) ;
     exit( 1 ) ;
}

5. fscanf( ) normally returns the number of items scanned and in the case of error a NULL is returned.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Hey,When you initialize it like that w/o any number, it is either an unknown number, or in most cases, 0.

Uninitialized is uninitialized, and always holds a junk value with the exception of global variables which are automatically initialized to 0.

Sharky, to avoid such subtle bugs *always* initizlize your variables when declaring it.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Hmm.. if the number of entries in the file can range from 8 to 99.. why bother creating a static array which will unnecessarily take up space. Why not do it in a more effective way:

char buffer[512] = { '\0' } ;

// keep on reading from file till EOF
while( fgets( buffer, 512, file_pointer ) )
{
   sscanf( buffer, ......
   // and so on..
}

This will make your code flexible, be it 99 or 999 lines.

As far as the display part is concerned, when you accept the input from user which is the song number, store it is a variable like "tmp_num".
Run a for loop through the entries of your array and if the "tmp_num" matches with any select[index] then print out an * in front of only "names[index]" .

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

These two loops have nothing in common. Change the while loop to look like the for loop.

Dont you think you are forgetting something in your code -- something like incrementign the index value so as to provide the terminating condition for the loop.

while(status !=EOF)
     {    
          time[index] = number;
          fgets(list, 99, inp);  
          strncpy(names[index], list, 28);
          names[index][29] = '\0';          
          status = fscanf(inp, "%d", &number);  
          ++ index ;
     }
}

Also If you sure that there are only 8 records then why not:

index = 1 ;

while( index < 9 )
{
  // your stuff goes here
  ++ index ;
}
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

If you are allowed to use inbuilt functions, why are you writing your own code for makeing the characters uppercase. Just use:

for( i = 0; i < MAX_LETTERS; ++i )
{
    letters[i] = toupper( letters[i] ) ;
}

This will make the entire contents of the array uppercase.

Loop through array with something like to count each character:

for( i = 0; i < num_used; ++i )
{
    count[ letters[i] ] ++ ;
}

Try this thing and repost.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

When the class "My" inherited from class "Base" it no longer remained a POD hence the different results. YOu can be rest assured that the ( ) notation always initializes the POD data.

Since inheritance is not supported in C, the moment My inherited from Base, it voilated the rule, since there is no equivalent of inheritance in C i.e. no conversion possible for the class My to make it C compatible.

Conclusion:

You can trust on ( ) to initialize "ONLY" POD and that too if the pointer is of type POD.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Hmm.. so it figures that the VS 2005 has the same output as the GCC compiler. Strange...

Better stick to the round bracket notation since it always seems to initialize the POD.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Hmm maybe it is because you must be using MS Visual studio. BTW I am using MingW GCC latest compiler.

I am attaching the executable, you can see for yourself that the thing works in my case:

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Okay to put it in a simple way, the round bracket syntax is used when you want to initialise the primitive data type varibles in your class with their defalut value while you ignore the brackets you just get an uninitialized object.

class My
{ 
   public:
      int a ;
} ;

int main( )
{
    My* first = new My ;
    My* second = new My() ;
   
    // this will print out junk i.e. uninitialized value
    cout << "value of var init without round brackets : " << first->a ;
   
    // this will print out 0 i.e. initialized value
    cout << "value of var init with round brackets : " << second->a ;
}

Similarly the round brackets beome mandatory if you want to pass values to the class constructor -- something you cant do by ignoring the round brackets.

If you want to see a full blown explanation you can look here

Hope it helped, bye.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Knock knock... do I see somthing missing...

#include <cstdlib> 
#include <iostream>
#include "Look.h"
using namespace std;

int currPos = 0;
int checkFlag = 0;
static int shipPos1 = 0; // default start position (pos 0)                                
int shipPos2 = 228;

// 3.80 minutes (228 seconds) 

Look::Look() {
}

Look::~Look() {
}

void Look::printShipPos() 
{ 
      if (checkFlag > 1) {
        shipPos = shipPos2 + 228; 
        cout << shipPos << endl;
                           }
        {    
// ouch
if (shipPos = 228) {
   std::cout << "French Polynesia [Out of Transmission Range]"<< endl; 
}
else if ((shipPos > 228) && (shipPos < 456)){
  std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl; 
}
else if ((shipPos >= 456) && (shipPos < 684)){
   std::cout << "Pacific Ocean (open waters) [Out of Transmission Range] 4320 miles from Tampa, Florida"<< endl; 
}

.
.
.

//---------------------counter for fly-over access
checkFlag++; 
cout <<checkFlag<< endl;

return;    
        }
}
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

The loss of indentation makes the code impossible to read and find the problem areas. It would be really better if you indented the code and resposted.

Some points:

1. WHy is the logic for counting the characters present in the function make uppercase? Why not create a seperate function for it?
2. Are you not allowed to use inbuilt functions ?

Answer the above questions and indent your code so that it becoems easy for me to help you out.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Please dont be vague. It doesnt work, doesnt convey us anything. State what kind of output were you expceting and what have you got.

BTW did you take a look at the prev post of Mr. Dragon, he pointed out some problem in your code, did you get it corrected ?

Also post your updated code.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

clock

clock_t clock(void);
The function returns the number of clock ticks of elapsed processor time, counting from a time related to program startup, or it returns -1 if the target environment cannot measure elapsed processor time.

You can find the aproximation between tick count of processor and seconds by wathching how many tick counts get elapsed in one second or something like that.

PS: Its almost 3.30 AM here so now I would be signing off. would solve your queries tomo. Bye.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Maybe you should read this.
http://www-ccs.ucsd.edu/c/time.html#clock

As far as the measuring of time is concerned, you can start the timer or measure the value of the clock at the start of the event and then query the time again when you want to know how many ticks has elasped.

// some code
double before = clock( ) ;
// some function here or some event started
double after = clock( ) ;

double time_elapsed = (after - before) / (the_scale_factor_of_your_game) ;

cout << "Time elapsed is " << time_elapsed << " since the magic spell was cast " ;

Hope it helped,bye.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Even i am happy seeing you in action.

Just wanted to let you know the one who starts the thread can mark the thread by clicking on the link "mark as solved" which lies at the left hand top side of your web page.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

So I hope your problem is solved ?
Can I mark this thread as solved ?

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

When I said printing out the individual elements of the character array, you should use the qualifier %c instead of %s in your print statement, since array elements are characters and not character pointers.

int string_length = strlen( new_number ) ;

      for( int j = string_length - 1; j >= 0; --j ){

  printf("Your number is:%s",new_number[j]);
      }
    return 0;

gives me an error is it the way i am printing?

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

No need to actually reverse the string -- unless your application demands so. Just traverse the array backwards using the for loop and everything should work out to be fine.

int string_length = strlen( number ) ;
for( int i = string_length - 1; i >= 0; --i )
{
    // output chars
}
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

>>system("cls");// is there anything else i can use to clear the screen?

No, there is as such no standard way to clear the screen. It is always compiler or OS dependent. So use it if you want, but your code will not be cross platform complaint.

I use scanf_s because my express compiler complains with scanf

Let it complain. scanf_s is not a standard C function.

new_number[k]=('0' + remainder); //Explain what u mean by "appending the remainder at the start"

What i mean is that:

You have a character array new_variable[50] = { '\0' } ; Now what you want to do is to extract out the remainder from the decimal number and convert it into a string.

while(decimal>0){

       /* Convert to Target Base*/
     
          remainder=decimal%targetbase;
          decimal=decimal/targetbase;
          new_number[k] = ('0' + remainder) ;
          --k;// I tried here but it prints garbage

  
          }

Instead of this, initialize k as k = 0 and start appending the remainder at teh start with something like:

int k = 0 ;

while(decimal>0)
{
   remainder=decimal%targetbase;
   decimal=decimal/targetbase;
   new_number[k] = ('0' + remainder) ;
   ++k ;
}

And display the number by looping through the array:

for( int i = 0; i <= k; ++i )
   // display each character
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Huh !!! What is the question ?
If you want to point out somethign in the code, highlight it so that i can know.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include <string.h>


int main(void)
{        
    /* Initialization section*/

    char number[50] = { '\0' } ; // Initialize MUST
    char new_number[50] = { '\0' } ; // Initialize MUST
    int sourcebase=0;
    int targetbase=0;
    int length=0;
    int decimal=0;
    int power=0;
    int remainder=0;
    
    
    do { 
           system("cls"); // dont use OS dependent funcitons

         printf("Enter a source base between 2 and 10:");
        scanf_s("%d",&sourcebase);
        if((sourcebase<2||sourcebase>10)){
        printf("Invalid!!!Your source base must be between 2 an 10\n\n");
             system("pause"); // dont use OS dependent functions
        continue;
        }
        printf("Enter a positive number:");

        scanf_s("%s",number,49); // dont know what it does
// it is not a standard C function.
 
        length=strlen(number);

        for(int i=0;i<length;i++){
            if((number[i]<'0')||(number[i]>=sourcebase+'0')){
            printf("Error!!! number cannot be less than zero.\n\n");
                system("pause");
        break;
            }
        
                if (i<length) continue;
        }
        printf("Enter a target base between 2 and 10:");
        scanf_s("%d",&targetbase);
        if((targetbase<2||targetbase>10)){
        printf("Invalid!!!Your target base must be between 2 an 10\n\n");
            system("pause");
        continue;
        }
        break;
        /* Validate User Data*/
    }while(1);
       /* Convert to Decimal*/
    for (int j=length-1; j>=0;--j){
        decimal+=(number[j]-'0')*pow((float)sourcebase,power);
        power++;
        
    } 

// int k=0; 
// WRONG !! if you want to add the remainder at the end of the
// character array you should initilize it with the last index of teh 
// character array and not 0

// Also adding remainder at the end of the array is difficult
// to achieve since the start positions of the char array might go
// wasted and print out junk while printing out the whole array.

// Better start appending the remainder at the start of char array.

       while(decimal>0){

       /* Convert to Target Base*/
     
          remainder=decimal%targetbase; …