Author Topic: C programming  (Read 3136 times)

0 Members and 4 Guests are viewing this topic.

Offline CujoTopic starter

  • Regular Contributor
  • *
  • Posts: 52
  • Country: au
C programming
« on: August 23, 2020, 07:20:27 am »
Why does this not print any char to the screen from the array? And how do I make each char to be a cap when printed?

int display_guesses( unsigned char guesses[] ){

  int i;

  for(i = 0; guesses != '\0'; i++)
  {
   printf("%c", guesses);
  }

//  return display_guesses_lib ( guesses ) ;
}
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2450
  • Country: us
Re: C programming
« Reply #1 on: August 23, 2020, 07:33:16 am »
Why does this not print any char to the screen from the array? And how do I make each char to be a cap when printed?

Code: [Select]
int display_guesses( unsigned char guesses[] ){

  int i;

  for(i = 0; guesses[i] != '\0'; i++)
  {
   printf("%c", guesses[i]);
  }

//  return display_guesses_lib ( guesses ) ;
}

Fooled me for a second...the code doesn't display right in forum if you don't put it in a code block (inserted above).

As to your second question, look up the toupper() library function.
The first question I don't know enough about your environment to answer, or whether the array you're passing is even populated with anything.
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22435
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: C programming
« Reply #2 on: August 23, 2020, 07:36:45 am »
You want to use the whole-ass printf() to write... single chars at a time? :o

Just use puts().

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline CujoTopic starter

  • Regular Contributor
  • *
  • Posts: 52
  • Country: au
Re: C programming
« Reply #3 on: August 23, 2020, 07:40:30 am »
Why does this not print any char to the screen from the array? And how do I make each char to be a cap when printed?

Code: [Select]
int display_guesses( unsigned char guesses[] ){

  int i;

  for(i = 0; guesses[i] != '\0'; i++)
  {
   printf("%c", guesses[i]);
  }

//  return display_guesses_lib ( guesses ) ;
}

Fooled me for a second...the code doesn't display right in forum if you don't put it in a code block (inserted above).

As to your second question, look up the toupper() library function.
The first question I don't know enough about your environment to answer, or whether the array you're passing is even populated with anything.

The array is populated in another function. The entire project is complete but we are required to rewire the code that is in the _lib file in our own code.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6404
  • Country: nz
Re: C programming
« Reply #4 on: August 23, 2020, 08:26:37 am »
Maybe you need to output a newline to make it flush the output buffer. Or do that explicitly with fflush(stdout) if you don't want a newline.

Or maybe there's no data in the array.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17774
  • Country: fr
Re: C programming
« Reply #5 on: August 25, 2020, 04:05:53 pm »
Most probable cause is hinted by Bruce above.

The output of printf() is usually not flushed to the console until it encounters a newline character.

The usual way of making sure each character is displayed here at each iteration would be to add a "fflush(stdout);" after the printf() call.

Edit: I focused on the printf() part (assuming this would be the culprit) and didn't even look at the rest... but obviously your use of the array was completely bogus. People below pointed you to what would be correct for your needs (which weren't even clear to begin with until you specified what the array really contained and what you wanted printed out.)

Still, you can keep in mind what I said above for later. Although it's implementation-defined, on most consoles, a printf() will not display anything until a "newline" (or end-of-file, I think, which is CTRL-Z?) character is encountered. So to print character by character, you'd need a fflush() call as I suggested. I don't remember if putc() suffers from the same behavior.
« Last Edit: August 26, 2020, 04:25:08 pm by SiliconWizard »
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 3270
  • Country: us
Re: C programming
« Reply #6 on: August 25, 2020, 07:36:18 pm »
I think you want:

Code: [Select]
void display_guesses(char *guesses)
{
   for (int i=0; guesses[i] != 0; i++) {
      if (guesses[i]>='a' && guesses[i]<='z')
         guesses[i] &= 0x5f;
      printf("%c", guesses[i]);
   }
}

void main(void)
{
   char testString[64]="Some test characters";

   display_guesses(testString);
}
« Last Edit: August 25, 2020, 10:14:50 pm by MarkF »
 

Offline CujoTopic starter

  • Regular Contributor
  • *
  • Posts: 52
  • Country: au
Re: C programming
« Reply #7 on: August 26, 2020, 05:14:59 am »
It's not an array of characters, it's an array of 1's and 0's. Each element of the array corresponds to a letter, 0 is 'a', 1 is 'b', etc, 25 is 'z'. So you need to check the entire array, and only print out the letters that have a '1' in their spot.

I'm too confused on how to do this?
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22435
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: C programming
« Reply #8 on: August 26, 2020, 05:41:00 am »
Do you mean guesses[0] corresponds to 'a', and so on; or do you mean a value of 0 in a given position corresponds to "a", and so on?

'1' is not in the range 'a' to 'z' (single quotes mean "use the numeric value of this character") so it's not obvious that that should do anything at all?

If the elements have numeric values 0-25 and you want to convert them to letters, use:
putc(guesses[ i ] + 'a');

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline CujoTopic starter

  • Regular Contributor
  • *
  • Posts: 52
  • Country: au
Re: C programming
« Reply #9 on: August 26, 2020, 06:19:26 am »
Do you mean guesses[0] corresponds to 'a', and so on;

That is exactly what I mean. So how do I implement that into my code?

Code: [Select]
int display_guesses( unsigned char guesses[] ){

  int i;

  for(i = 0; guesses != '\0'; i++)
  {
   printf("%c", guesses);
  }

//  return display_guesses_lib ( guesses ) ;
}
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22435
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: C programming
« Reply #10 on: August 26, 2020, 06:48:25 am »
So guesses[] is not so much char as bool type?  Only takes values 0 or 1?  (char can be used either way, but I mean semantically, how it's being used.)

Would that be a

Code: [Select]
if (guesses[i]) putc('a' + i);

in the loop then?

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 
The following users thanked this post: Cujo

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 3034
  • Country: gb
Re: C programming
« Reply #11 on: August 26, 2020, 06:51:46 am »
Do you mean guesses[0] corresponds to 'a', and so on;

That is exactly what I mean. So how do I implement that into my code?

Code: [Select]
#include <stdio.h>

int display_guesses(unsigned char *guesses)
{
  int i;

  for(i = 0; i < 26; i++)  {
    if (guesses[i] != 0)
      putchar(i + 'a');   
  }
  putchar('\n'); /* or fflush(stdout) if you don't want the newline. */
}

Ah, Tim beat be to it :)
 
The following users thanked this post: Cujo

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22435
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: C programming
« Reply #12 on: August 26, 2020, 07:00:30 am »
Note that if checks for nonzero, so the "!=" isn't necessary, but can help for clarity.

Note also that guesses[] needs to be size 26 or greater, otherwise the for loop breaks (reads gibberish memory); and you can't check for a zero terminator if zero is a normal value, i.e. it's not a string array.

Also I don't remember the difference between putc, putchar etc. because I mostly do embedded and don't use either. :P One of those (or other related) will do the job in any case.

Tim
« Last Edit: August 26, 2020, 07:02:13 am by T3sl4co1l »
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 3034
  • Country: gb
Re: C programming
« Reply #13 on: August 26, 2020, 07:22:48 am »
Note that if checks for nonzero, so the "!=" isn't necessary, but can help for clarity.

Cujo is clearly an inexperienced C programmer so I actually left in the check for clarity. I agree an experienced programmer would elide this.

Quote
Also I don't remember the difference between putc, putchar etc. because I mostly do embedded and don't use either. :P One of those (or other related) will do the job in any case.
putchar assumes stdout, putc/fputc takes the stream as a parameter.
 
The following users thanked this post: T3sl4co1l


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf