#simple error?
646 messages ยท Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
@restive nova
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
i made a thing to convert fareheit to cels
but in the example he got 2 numbers that are different
but veryy close
heres the code
int number_of_entries;
char numbers[200] = {};
char *Numbers = numbers;
int sum = 0;
printf("How many Numbers would you like to convert? \n");
scanf("%d", &number_of_entries);
printf("Please enter the numbers you would like to convert: ");
getchar();
fgets(numbers, 200, stdin);
int arrayNumbers[number_of_entries];
for(int i = 0; i < number_of_entries; i++) {
int fare = strtol(Numbers, &Numbers, 10);
int cels;
arrayNumbers[i] = fare;
cels = ((arrayNumbers[i] - 32) * 5.0/9.0);
printf("Cels: %d \n", cels);
}
return 0;
}
I am using the formula from F-> C
(x- 32 )* 5/9
you may want to round the formula's output so it does not get truncated before assigning it to cels
@idle brook ill try it
im a different james but 
i'm just being funny its fine
#rules
help out other people with their problems ๐
i know
react to the ๐ in #rules
in order to get a better role than "beginner" you have to demonstrate expertise by helping out other people with their problems
okayy
you think you can help me with this issue
i think im using the right formula
i just dont know why those 2 numbers are different
roundedCels = ceil(cels);```
and i did round it
up
too
converting a number to int rounds it toward 0
so (int) 75.6 is 75
should i do long
converting a number to long rounds it toward 0
you want to round the value before it is assigned to cels because that's when truncating happens
is
int fare = strtol(Numbers, &Numbers, 10);
so im guessing i change this from int to double
no, please stop guessin
im not guessing
strtol returns long
ok please stop for 1 second
ight
converting a number to int, long etc rounds it toward 0, and assigning a value to an int rounds it
so cels = ((arrayNumbers[i] - 32) * 5.0/9.0); converts (arrayNumbers[i] - 32) * 5.0/9.0 to int and rounds toward 0
gotcha
so if arrayNumbers[i] is 168 then 75.555555555556 rounds to 75
and then
ceil(cels) is ceil(75)
which is already rounded
you don't want to do ceil though
i forgot which one rounded accurately but i know ceil rounds up
ceil always rounds up
and floor rounds down
use round()
most likely what you want is cels = round((arrayNumbers[i] - 32) * 5.0/9.0);
oh
you don't haveto change the type of anything
the value that is stored in cels is truncated
"The round functions round their argument to the nearest integer value in floating-point format, rounding halfway cases away from zero"
๐
@restive nova Has your question been resolved? If so, run !solved :)
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
hey I have another question.
im trying to use fgets to gather information
but the problem is if i go over 20 for the character limit
it just lags my command prompt
scanf("%s", data);
fgets(data, 200, stdin);
getchar();
printf("your data is: %s\n", data);```
im trying to do 200
so if i enter " ffghfgh fgh fgh fgh fgh fgh fgh " it prints it
i don't think that's the problem
i think you meant to put getchar before fgets
one more thing, it just skips the first input. if i do 'a b c d' it just does 'b c d'
i tried moving around the getChar but nothing
no, scanf took the first input
how do i get it back from scanf

im new
lmao
i know scanf only reads the first input
thats why im using fgets
i didnt know it takes it tho
good to know
anyways
should i not use scanf at all then
yes, i think you can just remove scanf and getchar from that program
generally speaking, that's how input works
anything that reads input also "takes" it
You can only open threads you own
if i have an array
like
char data[] = { a b c d e }
can i print it like
%s, data[0]
to get a
okay
%c worked
but in my notes i have
%c" when scanning for operators (+-/*).
i guess i meant single characters
not operators
%c is for char
got it
stdio is originally designed to work on tapes
literal magnetic tapes
this fucking thing
that's why it reads things sequentially and has a "rewind" function
what does stdio do
records and plays back characters on tape access files
ah
this may sound really dumb but why is this for loop only printing the first value if i did i++
getchar();
fgets(data, sizeof(data), stdin);
printf("The %dth slot has a value of %c\n", i, data[i]);
}```
if data equals "f f f f"
shouldn't it print
the 0th slot is f
the 1th slot is f
etc
(4 is the number of entries)
if number_of_entries is 4 then it will wait for you to input four lines
and tell you the n'th entry of each separate line
i don't think fgets belongs in that loop
and i don't think getchar belongs anywhere
is that the same getchar i told you to get rid of?
you told me to put it on top
then how do i get it to repeat
with 1 line
so i said ^
oh i was mistaken then
if you don't want it to repeat then don't put it in the loop
if you don't want fgets to happen four times
the 1th slot is b
then don't put fgets in the loop that happens four times
ill take it all out of the loop
except the print
now its doing this
{
int number_of_entries;
char data[200] = {};
printf("How many entries will you have? ");
scanf("%d", &number_of_entries);
printf("Enter your data: ");
getchar();
fgets(data, sizeof(data), stdin);
printf("your data is: %s\n", data);
for (int i = 0; i < number_of_entries; i++) {
printf("The %dth slot has a value of %c\n", i, data[i]);
}
return 0;
}
if data equals "f f f f" then it will say The 1th slot has a value of
because there's a between the f and the f
better way to do what? can you please be a little more specific about what you want
so if my array is " a b c d"
i just want to be able to access things better
and if i can just do array[0,1,2,33,...]
that's about as unspecific as you could possibly be
yk what im saying
no i do not know what you're saying
hmm
so if your array is "a b c d" then you just want to access things better
how do i say this
lol
it sounds dumb
reading it
so my end goal is to take random groups of letters
and print out how many vowels there are
but im doing it inch by inch
input values: fghfghfgh fghfghfghfg fghfghfghf fghfgh
output: 0 0 0 0
since 0 vowels
but i want to try to figure that part out myself
i just dont know how to access each specific group of letters
don't shoot me, but
you could read with scanf instead of fgets
note that i said "instead of" and not "before" or "after"
i thought scanf only does the first grouping
yeah, and then the next scanf does the next one
oh in a for loop
ur sayiyng
but doesn't there need to be a comma in between them or no
no, there doesn't need to be a comma
you can also use fgets and then strtok. in that case you would not use scanf
{
scanf("%s", data);
printf("The %dth slot has a value of %c\n", i, data[i]);
}```
like this?
no
that would give you the first letter of the first word, then wait for you to put in the second word, and give you the second letter of the second word
how would i make it do the whole word then the next word
okay, can we stop and take a step back for a moment
yyep
so you want to read one whole line
correct
then split it into words
and then print the number of vowels in each word
wait sorry there could be more than one line too
abracadabra
pear tree
o a kak ushakov lil vo kashu kakao
my pyx
answer:
5 4 13 2
this is the example
but i dont want you to spoon feed me just kinda point me in the right direction
if posssible
ok, so you don't actually want to split this into words
its not like im doing homework or anything its just for my own benefit
right
because you want o a kak ushakov lil vo kashu kakao to give you 13 and not 1 1 1 3 1 1 2 3
correct
so i am guessing that you just want to read however many lines it gives you, right?
so first its going to ask how many lines are there
and then ill just put 4
for the example
then copy and paste that whole thing
abracadabra
pear tree
o a kak ushakov lil vo kashu kakao
my pyx
and then print out the vowels
still not really sure to do like if data.contains("a" , "e" "i" "o" "u" etc
ik in java
its .contains
for arrays
but idk in C
that isn't what you want
the way to do "contains" would be strstr or strchr
but that would only tell you whether there is an a
and you want to know how many
can't we make an array for integers and everytime we get a vowel
we add 1
to it
in the for loop
sure. why an array, though?
ig just an integer
ur right
a different integer
per line
so like int first
wait no that isn't a good idea
cuz
the real problem is 17 lines
so theres probably a better way to do it
im just trying to think out loud here because im not really sure
i was looking at this example
so, coming back to finding letters in the line:
char data[200];
for (int i = 0; i < 4; i++) {
fgets(data, 200, stdin);
int n_letters = strlen(data);
for (int x = 0; x < n_letters; x++) {
if (data[x] == 'a') puts("found a");
}
}
#include <string.h>
int main () {
const char haystack[20] = "TutorialsPoint";
const char needle[10] = "Point";
char *ret;
ret = strstr(haystack, needle);
printf("The substring is: %s\n", ret);
return(0);
}```
you don't want to use strstr here
it can tell you whether there's an a in the line
but not how many
ah
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
// using the %zu format specifier to print size_t
printf("Length of string a = %zu \n",strlen(a));
printf("Length of string b = %zu \n",strlen(b));
return 0;
}```
Length of string b = 7```
so strlen
can count all the letters
thats good
^
so nletters = the number of letters in data
or characters
number of characters
looking at this line if (data[x] == 'a') puts("found a");
ive never seen "puts" before
probably you don't want to print "found a" anyway
you want to count it
and you want to check for the other vowels too
yea
one step at a time
its good
if (data[x] == 'a') puts("found a");
this is basically sayying
if the 0th slot == a put a
ig grouuping doesn't matter
because obviously doesn't contain a vowel
so we can just do line by line
right
there is actually a trick you can do here
if (strchr("aeiou", data[x]))
which is like if "aeiou".contains data[x]
what does strchr do
note that it's strchr and not strstr
if "aeiou".contains data[x]
right, because aeiou and oiaeu both contain a
so for now
i was doing this
if (strchr("aeiouy", data[x])) {
printf("Got a vowel\n");
} else {
printf("No vowels.");
}
but before even giving the input
it just automatically says no vowels
i //'d it out
and the if statement works
so that is good
now we just need to tally them up
can you please show me the whole thing
this is it right now
actually i just moved this printf("Total Numbers: %d ", total_letters);
out of the if statement
#include <stdio.h>
#include <stdlib.h>
int main() {
int number_of_entries;
char data[200] = {};
printf("How many lines will you have? ");
scanf("%d", &number_of_entries);
printf("Enter your data: ");
for (int i = 0; i < number_of_entries; i++) {
fgets(data, 200, stdin);
int total_letters = strlen(data);
for (int x = 0; x < total_letters; x++) {
if (strchr("aeiouy", data[x])) {
printf("Total Numbers: %d ", total_letters);
}
}
}
return 0;
}
so, the stuff in for (int i happens for each line
and the stuff in for (int x happens for each letter
(or really each character)
and you want to count the vowels in each line
so that count should be inside for (int i
but not inside for (int x
before we do that i just have one question
sure
I put the printf("Total Numbers: %d ", total_letters);
here ill just send an updated thing
;format
again, you want to do that for each line but not each letter
so it should be inside for i
but not inside for x
and if you want to print how many vowels you counted you should do it after counting them, not before
ik i just wanted to make sure
the counter worked
so when you run the code
and it says
how many lines
and yoyu enter
it automatically sayys total number 1:
im just wondering how to get it to wait
until after you input "Enter your data:"
you are getting bit by mixing scanf and fgets
fgets goes until '\n' (return/enter key)
which is a space
so scanf doesn't take it
ok
if you type 2<enter>hello<enter> then
scanf reads "2"
first fgets reads "<enter>"
second fgets reads "hello<enter>"
#include <stdio.h>
#include <stdlib.h>
int main() {
int number_of_entries;
char data[200] = {};
printf("How many lines will you have? ");
scanf("%d", &number_of_entries);
printf("Enter your data: ");
for (int i = 0; i < number_of_entries; i++) {
fgets(data, 200, stdin);
int total_letters = strlen(data);
printf("Total Numbers: %d ", total_letters);
for (int x = 0; x < total_letters; x++) {
if (strchr("aeiouy", data[x])) {
// add now
}
}
}
return 0;
}
okay
you can put a scanf("\n") or getchar() after "enter your data"
scanf is adding a space?
no, it isn't
then why does it automatically \n
yea but thats way before
i'm not sure why you're fighting me on this
when you type "4<enter>" it goes "on the tape" and stays until scanf or fgets takes it
really?
why does it do that
its like its logging my enter
and then getchar takes it away
is C the only language that does that
no, python actually does that too
damn
you probably never dealt with bytewise i/o enough to notice
so whats the difference between fgets and scanf then if the enter is always on the table
or on the tape
fgets reads a line, including the <enter> at the end of it
scanf reads a word or number, not including anything after it (but potentially including some space before it)
yep so
wdym some space tho
if you type 1<enter>2<enter>3<enter> and read it with scanf
first scanf will read just "1"
second scanf will read "<enter>2"
third will read "<enter>3"
"<enter>2" is the same as "2" but scanf does in fact read the <enter>
scanf is a bit wonky and it becomes obvious when you try to mix scanf with fgets
because fgets assumes you are working with lines
and scanf doesn't
sure
so right now its almost done
it gets all the characters per line
so now we just use the strchr
aeiouy
thing
so how would i go about doing htat
obviously in here
{
if (strchr("aeiouy", data[x]))
{
// add now
}
}
int total_vowels = total_vowels+ 1;
is that good
no
int total_vowels means you're starting a new variable
and total_vowels = total_vowels+ 1; sounds like you're modifying an existing variable
right
total_vowels++;
you want to do vowels = 0 each line
so in the i loop
and if ... vowels++ each character
int total_vowels = 0;
in the i loop
total_vowels++
in the if statement in the x loop
yea
where would i print printf("Total Vowels: %d ", total_vowels);
i thought it would be outside the character inside the line
once per line, after you've finished counting the vowels
therefore in the "i" loop and after the "x" loop
#include <stdio.h>
#include <stdlib.h>
int main() {
int number_of_entries;
char data[200] = {};
printf("How many lines will you have? ");
scanf("%d", &number_of_entries);
printf("Enter your data: ");
getchar();
for (int i = 0; i < number_of_entries; i++) {
fgets(data, 200, stdin);
int total_letters = strlen(data);
int total_vowels = 0;
printf("Total Numbers: %d ", total_letters);
for (int x = 0; x < total_letters; x++) {
if (strchr("aeiouy", data[x])) {
total_vowels++;
}
}
printf("Total Vowels: %d ", total_vowels);
}
return 0;
}
how do i make it wait until all the lines are stated
before giving me the total vowels
rn it says it after each line
i just want it to say the total vowels at the end
per line
like 2 4 5 2
you would probably need to store it in an array instead of printing it right away
because then it would add them all
im gonna try the array
myself
ill let you know how it goes
ok, good luck
thanks
is this a task from the website?
yea
its like a problem solving website
List of programming problems and exercises from beginner to advanced level
okay let me ask you because im stuck on a part
i'm looking at the task and it doesn't actually say that you have to wait until the end to print the answer
input data:
4
abracadabra
pear tree
o a kak ushakov lil vo kashu kakao
my pyx
answer:
5 4 13 2
this is what they want
step1: int vowels[number_of_entries] = {}; this goes outside both loops
step 2; this goes inside the x loop
vowels[i] + 1;
no?
you accidentally the verb
i thought so hard onn that part
lol
because im taking the ith line
and adding one to it
no?
... and store the result in the i'th slot, right?
+= is the same as vowels[i] = vowlels[i] + 1
right
okay
step 3
this is where im confused
how do i print the whole array
at once
printf("Total Vowels: %d ", vowels[???]);
what does in there
g
for (int i = 0; i < number_of_entries; i++) printf("Total Vowels: %d ", vowels[i]);
it goes in the i loop?
nooo
give it its own loop
but... i don't think you are supposed to say Total Vowels : 5 Total Vowels : 4 Total Vowels : 13 Total Vowels : 2
ik
just %d
but im confused
on making its own loop
wouldn't that just print it more than once
oh
im dumb
thats what we want
we want it to print the whole thing
so this loop will print every slot in the array
ur so smart
1 issue
int vowels[number_of_entries] = {};
it says numbers_of_entries
isn't initialized
for the vowels part
how would it not be initiliazed
if it gets the answer right there
in the circle
๐
if scanf("%d", &number_of_entries) failed, number_of_entries would not be initialised

lol
so what should i do
u think
just put like 20
or 100
or something
i mean it doesn't really matter
i just thought it would be cool to match the size exactly
the "right" thing to do is actually test whether scanf succeeds
but if it isn't going to fail then you can just get away with putting 1
no
yeah, so that it's initialised
ah
lets see if it works
๐
wait same thing
i even did 10
@thorny quarry do you know why
same error
also what IDE do you use
im using code blocks rn
tried VSC but it was being weird
i don't use an ide 
what do u use
it should tell you which line
compiler and text editor
those two lines should not produce that error
thats what im saying
are there any other lines in the program?
#include <stdlib.h>
int main()
{
int number_of_entries = 1;
int vowels[number_of_entries] = {};
char data[200] = {};
printf("How many lines will you have? ");
scanf("%d", &number_of_entries);
printf("Enter your data: ");
getchar();
for (int i = 0; i < number_of_entries; i++)
{
fgets(data, 200, stdin);
int total_letters = strlen(data);
for (int x = 0; x < total_letters; x++)
{
if (strchr("aeiouy", data[x]))
{
vowels[i] += 1;
}
}
}
for (int y = 0; y < number_of_entries; y++)
{
printf("%d ", vowels[y]);
}
return 0;
}
thats the whole thing
;format ```#include <stdio.h>
#include <stdlib.h>
int main()
{
int number_of_entries = 1;
int vowels[number_of_entries] = {};
char data[200] = {};
printf("How many lines will you have? ");
scanf("%d", &number_of_entries);
printf("Enter your data: ");
getchar();
for (int i = 0; i < number_of_entries; i++)
{
fgets(data, 200, stdin);
int total_letters = strlen(data);
for (int x = 0; x < total_letters; x++)
{
if (strchr("aeiouy", data[x]))
{
vowels[i] += 1;
}
}
}
for (int y = 0; y < number_of_entries; y++)
{
printf("%d ", vowels[y]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number_of_entries = 1;
int vowels[number_of_entries] = {};
char data[200] = {};
printf("How many lines will you have? ");
scanf("%d", &number_of_entries);
printf("Enter your data: ");
getchar();
for (int i = 0; i < number_of_entries; i++) {
fgets(data, 200, stdin);
int total_letters = strlen(data);
for (int x = 0; x < total_letters; x++) {
if (strchr("aeiouy", data[x])) {
vowels[i] += 1;
}
}
}
for (int y = 0; y < number_of_entries; y++) {
printf("%d ", vowels[y]);
}
return 0;
}
Powered by godbolt.org
;compile
;compile C ```#include <stdio.h>
#include <stdlib.h>
int main()
{
int number_of_entries = 1;
int vowels[number_of_entries] = {};
char data[200] = {};
printf("How many lines will you have? ");
scanf("%d", &number_of_entries);
printf("Enter your data: ");
getchar();
for (int i = 0; i < number_of_entries; i++)
{
fgets(data, 200, stdin);
int total_letters = strlen(data);
for (int x = 0; x < total_letters; x++)
{
if (strchr("aeiouy", data[x]))
{
vowels[i] += 1;
}
}
}
for (int y = 0; y < number_of_entries; y++)
{
printf("%d ", vowels[y]);
}
return 0;
}
How many lines will you have? Enter your data: 0
<source>: In function 'main':
<source>:24:29: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
24 | int total_letters = strlen(data);
| ^~~~~~
<source>:3:1: note: include '<string.h>' or provide a declaration of 'strlen'
2 | #include <stdlib.h>
+++ |+#include <string.h>
3 |
<source>:24:29: warning: incompatible implicit declaration of built-in function 'strlen' [-Wbuiltin-declaration-mismatch]
24 | int total_letters = strlen(data);
| ^~~~~~
<source>:24:29: note: include '<string.h>' or provide a declaration of 'strlen'
<source>:30:17: warning: implicit declaration of function 'strchr' [-Wimplicit-function-declaration]
30 | if (strchr("aeiouy", data[x]))
| ^~~~~~
<source>:30:17: note: include '<string.h>' or provide a declaration of 'strchr'
<source>:30:17: warning: incompatible implicit declaration of built-in f
;help
For help with a specific command, type ;help <command>
Struggling? Check out our wiki
Grabs the bot's invite link
Compiles a script
Displays the compilers for the specified language
Displays all supported languages
Outputs the assembly for the input code
Displays information about the bot
See ;help cpp for more info ```
Formats code using a code formatter (i.e. clang-format or rustfmt)
Displays all formatting options & styles
;help compile
how do you include an input
;compile c 5```#include <stdio.h>
#include <stdlib.h>
int main()
{
int number_of_entries = 1;
int vowels[number_of_entries] = {};
char data[200] = {};
printf("How many lines will you have? ");
scanf("%d", &number_of_entries);
printf("Enter your data: ");
getchar();
for (int i = 0; i < number_of_entries; i++)
{
fgets(data, 200, stdin);
int total_letters = strlen(data);
for (int x = 0; x < total_letters; x++)
{
if (strchr("aeiouy", data[x]))
{
vowels[i] += 1;
}
}
}
for (int y = 0; y < number_of_entries; y++)
{
printf("%d ", vowels[y]);
}
return 0;
}
<source>: In function 'main':
<source>:24:29: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
24 | int total_letters = strlen(data);
| ^~~~~~
<source>:3:1: note: include '<string.h>' or provide a declaration of 'strlen'
2 | #include <stdlib.h>
+++ |+#include <string.h>
3 |
<source>:24:29: warning: incompatible implicit declaration of built-in function 'strlen' [-Wbuiltin-declaration-mismatch]
24 | int total_letters = strlen(data);
| ^~~~~~
<source>:24:29: note: include '<string.h>' or provide a declaration of 'strlen'
<source>:30:17: warning: implicit declaration of function 'strchr' [-Wimplicit-function-declaration]
30 | if (strchr("aeiouy", data[x]))
| ^~~~~~
<source>:30:17: note: include '<string.h>' or provide a declaration of 'strchr'
<source>:30:17: warning: incompatible implicit declaration of built-in f
;compile c 5```cpp
#include <stdio.h>
#include <stdlib.h>
int main() {
int number_of_entries = 1;
int vowels[number_of_entries] = {};
char data[200] = {};
printf("How many lines will you have? ");
scanf("%d", &number_of_entries);
printf("Enter your data: ");
getchar();
for (int i = 0; i < number_of_entries; i++) {
fgets(data, 200, stdin);
int total_letters = strlen(data);
for (int x = 0; x < total_letters; x++) {
if (strchr("aeiouy", data[x])) {
vowels[i] += 1;
}
}
}
for (int y = 0; y < number_of_entries; y++) {
printf("%d ", vowels[y]);
}
return 0;
}
why did you move int vowels[number_of_entries] = {}; before scanf
the error i get is "variable length array cannot be initialised"
it means you can have
int vowels[number_of_entries];
or
int vowels[100] = {0};
but not
int vowels[number_of_entries] = {0};
yes, it matters
well that's bad
i was going to say
the reason it matters
int vowels[number_of_entries]; will be uninitialised
technically int vowels[100] = {}; is not allowed.
they are the same, except int vowels[100] = {}; won't work sometimes
it worked
what does the 0 do
you just have to specify at least one value
int vowels[100] = {0}; is the same as int vowels[100] = {0,0,0,0,0,0,0,}; etc
its just not empty
are you curious how other people did this problemm in C?
#include <string.h>
#include <math.h>
int main(void)
{
char lineSymbols[70];
char divider[2]=" ";
char *istr;
FILE *file;
char *line;
file = fopen("input.txt","r");
if(file==NULL){
puts("cannot open file");
}
while((line=fgets(lineSymbols,70,file))!=NULL){
int i,count=0;
for(i=0;i<(strlen(line));i++){
if((*(line+i)=='a')||(*(line+i)=='o')||(*(line+i)=='u')||(*(line+i)=='i')||(*(line+i)=='e')||(*(line+i)=='y')){
count++;
}
}
printf("%i \t",count);
/*printf("%s \n ",line);*/
}
fclose(file);
return (0);
}
he cheated
he put it in a file
why would he do this
he could of done what u did
lmao
anyways
time to go to sleep
thanks for helping me better understand this all