C loop, counter [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So I'm trying to make this like a counter with two sets of digits, my logic seems right, it will increment till nine then the counter will "flip" the next digit and so on and so forth. When I run it though I just get pages of zeroes, I was wondering if anyone can help or point me in the right direction.
char ft_putchar(char c)
{
write(1, &c, 1);
return 0;
}
void ft_print_comb2(void)
{
char num1a = '0';
char num1b = '0';
char num2a = '0';
char num2b = '0';
while (num1a != '9' && num2a != '9')
ft_putchar(num1a);
ft_putchar(num1b);
ft_putchar(',');
ft_putchar(' ');
ft_putchar(num2a);
ft_putchar(num2b);
num2b++;
if (num2b == '9')
{
num2b ='0';
num2a++;
}
if (num2a == '9')
{
num2a ='0';
num1b++;
}
if (num1b == '9')
{
num1b='0';
num1a++;
}
}

You while has a single line body which is ft_putchar(num1a); due to the missing braces {..}
Try it like so
void ft_print_comb2(void)
{
char num1a = '0';
char num1b = '0';
char num2a = '0';
char num2b = '0';
while (num1a != '9' && num2a != '9')
{
ft_putchar(num1a);
ft_putchar(num1b);
ft_putchar(',');
ft_putchar(' ');
ft_putchar(num2a);
ft_putchar(num2b);
num2b++;
if (num2b == '9')
{
num2b ='0';
num2a++;
}
if (num2a == '9')
{
num2a ='0';
num1b++;
}
if (num1b == '9')
{
num1b='0';
num1a++;
}
}
}

Related

I want to write clean code in general not just C [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 days ago.
Improve this question
I'm taking the course CS50 and in my early stage of learning how to code I want to have a habit of writing clean code. Can you check out my code below and give some advice on what am I doing wrong or what am I not seeing.
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
bool valid(string password);
int main(void)
{
string password = get_string("Enter your password: ");
if (valid(password))
{
printf("Your password is valid!\n");
}
else
{
printf("Your password needs at least one uppercase letter, lowercase letter, number and symbol\n");
}
}
// TODO: Complete the Boolean function below
bool valid(string password)
{
bool alphanumeric = false;
bool uppercase = false;
bool lowercase = false;
bool symbol = false;
for (int i = 0, n = strlen(password); i < n; i++)
{
// Checks for an uppercase letter
if (password[i] >= 'A' && password[i] <= 'Z')
{
uppercase = true;
}
// Checks for a lowercase letter
if (password[i] >= 'a' && password[i] <= 'z')
{
lowercase = true;
}
// Checks for numbers
if (password[i] >= 48 && password[i] <= 57)
{
alphanumeric = true;
}
// Checks for symbols
if ((password[i] >= 33 && password[i] <= 47)
|| (password[i] >= 58 && password[i] <= 64)
|| (password[i] >= 91
&& password[i] <= 96) || (password[i] >= 123 && password[i] <= 126))
{
symbol = true;
}
}
if (alphanumeric == true && uppercase == true && lowercase == true && symbol == true)
{
return true;
}
else
{
return false;
}
}
Can you check out this code I've written and fix/recommend on how I can modify my thought process in writing clean code?

Reading lines from file, printing only lines that have same amount of letters as user will choose [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to write code where you will choose words that will be printed if they have same amount of letters that you will choose.
If you choose that you want to have printed only words that have 3 letters it will ignore longer or shorter words.
I have no idea, how to write a word if letter count is same as user wanted.
This is what I have so far.
do
{
ch = fgetc( fp );
if ( ch == ' ' || ch == EOF || ch == '\n' )
{
if(count == n) {
/* Here should be printed the word */
count = 0;
}
else count = 0;
}
else
{
++count;
}
} while ( ch != EOF );
You must store the letters in an array so that you can print the content of the array when needed.
char word[256];
do
{
ch = fgetc( fp );
if ( ch == ' ' || ch == EOF || ch == '\n' )
{
if(count == n) {
/* Here should be printed the word */
word[count] = '\0'; // terminate word
printf("%s\n", word); // print word
count = 0;
}
else
count = 0;
}
else
{
word[count] = (char)ch; // save the characters
++count;
}
} while ( ch != EOF );

why does this c program print twice [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
#include <stdio.h>
int main()
{
char c = 'A';
while (c != ',')
{
printf("Input a character:");
scanf("%c", &c);
if (c >= '0' && c <= '9')
{
printf("%d\n", (int)c);
}
}
}
After taking in the first set of input, this code prints out "Input a character" twice each time - why is this?
cause you press a number PLUS enter and enter will be read by scanf() at the next call
#include <stdio.h>
int main(void) {
char c = 'A';
while (c != ',') {
printf("Input a character:");
if (scanf("%c", &c) != 1) {
return 0; // we stop if user don't input anything
}
if (c >= '0' && c <= '9') {
printf("%d\n", (int)c); // by the way did you want (int)(c - '0') ?
} else {
printf("enter a number ! you enter %d\n", c);
}
}
}

I've got an issue with my Enigma simulation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
void character(){
int i=0;
char c;
printf("type your text to encode (max 80 chars):\n");
while((c=getchar()) != '\n')
{
text[i] = toupper(c);
i++;
}
text[i] = '\0';}
I'm using this piece of code in an emulator of Enigma. My problem is that the While instruction is always jumped, and I can't understand what's the problem and how to fix it!
Maybe there is something wrong with the declaration of the array text. This solution could clarify your question.
#include <stdio.h>
#include <ctype.h>
#define MAX_SIZE 10
int main(void) {
char text[MAX_SIZE];
int i = 0;
int c;
while ((c = getchar()) != '\n' && i <= MAX_SIZE - 2) {
text[i] = toupper(c);
i++;
}
text[i] = '\0';
printf("%s\n", text);
return 0;
}
As #trentcl mentioned, if there is a scanf prior to calling character() there is probably a newline left in the input stream. This will test for a leading newline and continue the loop.
The #define will set the size of the array so the loop does not put too many characters into text.
#define LIMIT 256
char text[LIMIT + 1] = {'\0'};
void character(){
int i = 0;
int c;
printf ( "type your text to encode (max 80 chars):\n");
while ( ( c = getchar ( )) != EOF)
{
if c == '\n') {
if ( i) {
break;//trailing newline
}
else {
continue;//leading newline
}
}
text[i] = toupper ( c);
i++;
if ( i >= LIMIT) {
break;
}
}
text[i] = '\0';
}

Expression syntax error issue [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I was writing a simple code to change from lowercase to uppercase but its showing expression syntax error on a line 12 in the if() statement. The code runs fine without equality sign but does not work with equality. Any help to rectify it is appreciated.
The code is as follows:
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char s[]="Computer";
int i;
clrscr();
for (i=0;i<strlen(s);i++)
{
if (s[i] > = 'a' && s[i] < = 'z')
s[i]+='A'-'a';
}
puts(s);
getch();
return 0;
}
This > = is not valid syntax because of the space between > and =. Delete the space :
if (s[i] >= 'a' && s[i] <= 'z')
The syntax error you are getting is due to the space between > and = operators. Likewise for < and =. Also, this would be a better version of your code:
int main()
{
char s[] = "Computer";
size_t i, len = strlen(s);
for (i = 0; i < len; i++) {
if (s[i] >= 'a' && s[i] <= 'z')
s[i] -= 32;
}
puts(s);
getch();
return 0;
}
Please look your code in if(s[i] > = 'a' && s[i] < = 'z')
as if(s[i] >= 'a' && s[i] <= 'z')

Resources