unexpected ./ output when coding in C - c

I'm just starting out in learning to code in C using edX and I am sure this is a really simple mistake, which I can't spot.
I am getting an extra ./ which I can't work out where it is coming from.
This is what I get as the output
How many minutes does your shower last: 3
minutes: 3
bottles: 36./
I am using the CS50 IDE on Cloud 9 and my code is in the screenshot
Your help is appreciated.
This is my code:
int main(void)
{
int showerlength, bottle;
printf("How many minutes does your shower last: ");
scanf("%d", &showerlength);
bottle = (showerlength * 12);
printf("minutes: %d\n",showerlength );
printf("bottles: %d\n",bottle );
}
This is the output I get
~/workspace/pset1/ $ ./water
How many minutes does your shower last: 3
minutes: 3
bottles: 36./

This does not look like an issue anymore.
I tested exactly the same code in CS50 IDE, and output is fine.
I even tested it with and without return(0) in the main function. It works fine.
Screenshot

This would not be a problem anymore. But, try the same thing in the new CS50 IDE, at ide.cs50.io.

Related

In what situation the output could go wrong like this?

I am trying to do the problem 200B on codeforces. I tested my code, and the output was all right. But when I uploaded it to the online judge system, I failed on the very first test case. It said my output was -0.000000000000, instead of 66.666666666667.
But I have compiled and run on Visual Studio C++ 2010, MacOS clang 13.0.0, and Linux GCC 6.3.0, the outputs were all the same as mine, 66.666666666667. I am very curious and want to figure out in what situation the output could be -0.000000000000.
On my computer,
Input:
3
50 50 100
Output:
66.666666666667
On the online judge system,
Input:
3
50 50 100
Participant's output
-0.000000000000
Jury's answer
66.666666666667
Checker comment
wrong answer 1st numbers differ - expected: '66.66667', found: '-0.00000', error = '1.00000'
#include <stdio.h>
int main(void)
{
int n;
double sumOrange = 0;
double sumDrink = 0;
scanf ("%d", &n);
while (n-- > 0) {
int m;
scanf("%d", &m);
sumOrange += m / 100.0;
sumDrink++;
}
printf("%.12lf\n", (sumOrange / sumDrink) * 100.0);
return 0;
}
I just don't understand why my output could be -0.000000000000. Please help, thanks.
Update: Tested on different versions of GCC (4.9, 5.1, 6.3), the wrong output does not appear. Guess the cause might lie in the specific implementation of printf.
The problem is because printf function in GNU gcc C11 does not support %.12lf format. It should be changed to %.12f For more information, you can read the article below:
Correct format specifier for double in printf

Execution of code stuck on running in VSCode

So I have preinstalled extension like 'Code Runner' and 'C/C++' in Vscode.
The problem I am facing is in running a code which gets stuck on running :
#include <stdio.h>
#include <stdlib.h>
int main()
{
//program to convert minutes into years and days
int min = 0;
double yrs = 0.0;
double days = 0.0;
double min_in_yr = 0;
printf("Enter the nmuber of minutes: ");
//get input from the user
scanf("%d", &min);
//calculation
min_in_yr = (60 * 24 * 365);
yrs = (min / min_in_yr);
days = (yrs * 365);
printf("%d minutes is approx %f years and %f days\n", min, yrs, days);
return 0;
}
There is no problem with the code as it seems to be running fine on CodeBlocks.
Also I tried executing a simple hello world program which seemed to be executed perfectly on Vscode in 1.8 s.
What could be the problem?
So I looked up and found a solution for this problem.May it helps other beginners who get stuck doing this on Vscode.
So the problem is actually with the scanf function which seems to be not compatible with OUTPUT section of the output panel.I don't know the reason for this but I found a alternate way to do this.
Just make your code run in the terminal instead.
To do this:
1.Make sure you have code runner extension installed
2.Goto File>Preferences>Settings>Extensions>Run Code configuration and under that scroll down to search for Code Runner:Run in Terminal and check that option.If you are not able to find that search in settings for text in bold and you find
3.That's it.Run your code and it will automatically be executed in the terminal

Problem with running and compiling c with Git Bash on Windows

screenshot
I'm fairly new to all of this, so if it is possible could help be really dumbed down for me thank you :).
So I've had about 1 semester of coding at Uni, and we were learning C. Wanting to practice during my holiday I've done some research and opted to download Atom, installed the Gpp-compiler packages, MinGw and created a path, installed git bash etc. according to all the online instructions I could find.
However when I finally thought I was done with all the set up, a problem occured. As I was testing things out, I realised I couldn't run things properly like I was doing on the uni terminal and gedit.
Shown in my screenshot, shouldn't "hello world" be printed first after I try to run it, then I could enter a value and it should print the value or something.
However after running it would come up blank, until I enter something random and then it would print all at once.
Not sure what's happening here or what I did wrong, any guidance would be great thank you.
Edit: this only seems to be a problem, when I use scanf. Without it, everything prints out in order.
My code:
#include <stdio.h>
int main (void) {
int value;
printf("hello world\n");
scanf("%d", &value);
printf("%d", value);
return 0;
}
when I try to call, it comes out blank until I input 45, then it prints out "hello world" "45"
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ gcc -o main main.c
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ ./main
45
hello world
45
Another example:
#include <stdio.h>
#include <math.h>
int main(void) {
double sideA, sideB, sideC;
double s, area;
printf("Please enter 3 sides of your triangle: \n");
scanf("%lf %lf %lf", &sideA, &sideB, &sideC);
s = (sideA + sideB + sideC) / 2;
area = sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
printf("%lf\n", area);
return 0;
}
Output: nothing came out until I entered (2 3 4) and (5.31 4.2 7.77)
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ ./heron
2 3 4
Please enter 3 sides of your triangle:
2.904738
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ ./heron
5.31 4.2 7.77
Please enter 3 sides of your triangle:
10.542172

Printing Out Timesteps For Game of Life

I am writing a Game of Life code and that simulation will run for 1000 timesteps. The program seems to be running well, but I want to add another feature where the timesteps will be printed out on top of the current state of the board. For example, on timestep = 0, the initial board will be printed out and on timestep = 1000 the final state of the board will be printed out.
I have thought of the following code to print out the timestep (note: I am still a beginner at learning C programming):
int timestep = 1000;
printf("Timestep: ");
for (int init_time = 0; init_time < timestep; init_time++)
{
printf("%d", init_time);
}
I am not sure whether this code will print out this way: Timestep Sample Image (Click Here)
I am thinking that a row needs to be cleared out to print out the timestep, but I am not sure how to do that or if this is even the correct approach to solving that issue. I will appreciate any kind of help that you can offer.

gcc:error in input

i am enthusiast and new in programming trying this simple c language code and compile it with gnu compiler. here is my code:
/*simple program:trying to printf string and get a letter*/
#include "stdio.h"
int main()
{
int i=0;
char c;
while(i++<100){
if(i % 2)
printf("%C this is even number",i);
if(i==50){
c=getchar();
printf("you enter %c letter",c);
}
}
return 0;
}
however when i compile it with gcc and run in terminal, it doesnt show anything,no error and no warning. i tried to figure it out by changing the way my code behave but still not work. is it the code that wrong or gcc have bug or may be i miss about something?
Try to put a newline at the end of printf text:
printf("%C this is even number\n",i);
Also i is an int so use the %d format:
printf("%d this is even number\n",i);
Edit: I just tested this with the changes and it works for me.
You should call fflush(stdout) after your display.
It ran for me using cygwin under Windows Vista.
I pasted your code into a file called test.c and ran gcc test.c that output a file called a.exe which I then ran.
johnma#johnma-PC ~
$ ./a.exe
this is even number♥ this is even number♣ this is even number this is even numb
this is even numbern this is even number◄ this is even number this is even numb
er this is even number this is even number↓ this is even numberthis is even numb
er this is even number this is even number! this is even number# this is even nu
mber% this is even number' this is even number) this is even number+ this is eve
n number- this is even number/ this is even number1 this is even number
you enter
letter3 this is even number5 this is even number7 this is even number9 this is
even number; this is even number= this is even number? this is even numberA this
is even numberC this is even numberE this is even numberG this is even numberI
this is even numberK this is even numberM this is even numberO this is even numb
erQ this is even numberS this is even numberU this is even numberW this is even
numberY this is even number[ this is even number] this is even number_ this is e
ven numbera this is even numberc this is even number
johnma#johnma-PC ~

Resources