I am trying to run the following simple code to check if scanf() is working.
#include<stdio.h>
int main(){
int num=0;
scanf("%d",&num);//Scanf not working
printf("The number is %d",num);
return 0;
}
However, it gives me an output of zero even though I enter some value for the variable 'num'.
For further clarification, I just installed turbo c++ in my new laptop. Is this causing some problem?
Related
I was trying to run a C++ program in VS Code.
Program:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int n;
cout<<"enter the no "<<endl;
cin>>n;
int ans=0,i=0;
while(n!=0){
int bit=n&1;
ans= (bit * pow(10, i) ) + ans;
n=n>>1;
i++;
}
cout<<ans;
}
This program isn't working in my pc. But whenever I try to run same program in online compiler on another pc, It works fine.
Like If I enter the input as 6, then it prints 109 instead of 110.
Why is the program not working properly on my PC?
I also tried to debug to check the program but Debugger prints the output to Debug Console even though ExternalConsole is set to true. From Debug Console, It doesn't take the input. Giving the error, Unable to perform this action because process is running.
Why my vscode prints on Debug Console instead of ExternalConsole?
I'm trying to run a simple C program in Eclipse and I am new to Eclipse.
When running the command I see output of the program for a very brief moment in the console, but instead for the program to wait for input, output disappears and and an empty console is shown instead. Is this a problem of the program or something related to the settings of the Eclipse IDE?
Edit: On the console menu there is a terminate button and when I pres it the output of the program shows but it doesn't wait for input and the program is terminated. Any help?
Image:
Here is the code
//The input is a series of numbers and the number X. Find
//how many times the number X is met in the series
#include<stdio.h>
int main(){
int m[20];
int n, X, cnt=0;
printf("Insert maximum length of the series (n<=20)\n");
scanf("n= %d",&n);
printf("Insert X the number you are looking for\n");
scanf("X= %d",&X);
while(n<1||n>20){
printf("Bad input, n should be between 1 and 20\n");
scanf("n= %d",&n);
}
for(int i = 0 ; i < 20 ; i++){
scanf(" %d", &m[i]);
if(m[i]==X)cnt++;
}
printf("X is found %d times\n", cnt);
return 0;
}
and here is the empty console:
Edit: I've tried rewriting the code using cout and cin but then the compiler in Eclipse complains on the line #include<iostream.h> saying that there is an unresolved inclusion. Sounds like a separate question to me.
The answer is here: click
Basically the output of the standard input output is buffered and setvbuf(stdout, NULL, _IONBF, 0); solves the problem.
I was trying to read the last entry of a binary file containing structures.
(I'm working on Turboc++ because my school uses it to teach us)
I tried doing seekg(-sizeof(info),ios::end) , where info is the structure, but it doesn't give me the desired output.
But when i do seekg(-4,ios::end) , (info is of size 4) it works fine.
Even seekg(sizeof(info),ios::beg) works fine (Did that to check if I'm using sizeof() in the wrong way)
The following program (which I wrote on turboc++) demonstrates the problem I'm facing (though it's not the actual program I'm working on)
#include<iostream.h>
#include<fstream.h>
struct info{
int age, num;
};
int main()
{
info p1,p2;
ofstream fout("NEWF",ios::out|ios::binary|ios::trunc);
ifstream fin;
p1.num=2; p1.age=20;
fout.write((char*)&p1,sizeof(info));
p1.num=4; p1.age=20;
fout.write((char*)&p1,sizeof(info));
fout.close();
fin.open("NEWF",ios::in|ios::binary);
fin.seekg(-sizeof(info),ios::end); //trying to access last entry
// int nsize= -sizeof(info);
// fin.seekg(nsize,ios::end); <--- This works
fin.read((char*)&p2,sizeof(info));
cout<<p2.num<<" "<<p2.age;
return 0;
}
The output I'm getting is: 1532 1556 ,
While the expected output is : 4 20
I did go through many similar questions already asked by people related to seekg() but I couldn't find what I was looking for.
I am making a simple program to add two numbers. I've done everything correct but somehow it's showing an error.
I am using TurboC for windows7 64bit (downloaded from filezilla)... I've also used devcpp but but there its showing error in using void main()... Why this is so? Why it is not working?
Also, can anybody suggest some good software for programming console-based for projects C, C++, etc.?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num1=0,num2=0;
//printing hello world
//printf("Hello World!");
printf("Enter number 1 : ");
scanf("%d",num1);
printf("Enter number 2 : ");
scanf("%d",num2);
int num3 = num1+num2;
printf("The sum of %d and %d is %d",num1,num2,num3);
getch();
}
The problem is with your scanf(). When accepting values, you must add the & before the variable. The unary & returns the address of the variable next to it, and scanf() then stores the value at that address. But note that you do not need to use & in printf() unless you actually want to print the address. In short, change your scanf() 's to
scanf("%d",&num1);
and
scanf("%d",&num2);
Here's your working code code
#include<stdio.h>
int main()
{
int num1=0,num2=0;
//printing hello world
//printf("Hello World!");
printf("Enter number 1 : ");
scanf("%d",&num1); // see here
printf("Enter number 2 : ");
scanf("%d",&num2); // and here
int num3 = num1+num2;
printf("The sum of %d and %d is %d",num1,num2,num3);
}
The error with void main() is that it is no longer accepted. On older versions like TurboC, you can use void main(), but the standard clearly states that we should not use void for main(), instead you should use int main().
Read this for reference
What should main() return in C and C++?
And, don't use <conio.h>. It's not supported in the standard. If you want to clear the screen, add the header file <stdlib.h> and use system("cls");
Regarding a replacement for getch(), you can just use getchar(). ( although in some programs, you will have to use two or more getchar()'s )
There's one thing you should know, and that is that both TurboC and DevC++ are outdated.
You should probably get Code Blocks.
You get the "Declaration not allowed here" error because prior to C99 ( your IDE TurboC runs on an older version than C99 ) , variables had to be declared at the beginning of a block. You can use Declaration not allowed here error in C as reference
I'm only focusing on the error:
printf("Enter number 1 : ");
scanf("%d",&num1); //use & for input
printf("Enter number 2 : ");
scanf("%d",&num2); //use & for input
When you use scanf(), you must provide the address of the variable you write to using &.
scanf("%d",num1);
should become:
scanf("%d",&num1); //add the & to refer to the address
In case the title is not precisely revealing what I want to do, the following is my problem.
I want to write a c++ program in a Linux or Mac terminal to print numbers that keeps counting from 1, 2, 3 ... at the same position under a command line mode. For example, it is like displaying the number of percentage when your work is progressing(e.g. downloading something, installing software...).
I wrote a simple for-loop to print numbers and use usleep(1000); for a delay of 1 second before printing next number. Then I use cout << "\b"; trying to move cursor back to display coming number at the same position. However I fail to create the effect that I want, the numbers are printed in a line.
I am not a skillful c++ programmer and know very limited on programming in a terminal environment. Can anyone help to give me hint or sample code for this function? Thanks!!
If you are in Linux Terminal you can also use the following code,
system("clear");
cout<<"\b";
cout<<Your_Number;
// Repeat this in a loop and call the delay function
This works in terminal for me (am using linux)
#include
#include
using namespace std;
int main(int argc, char *argv[]) {
int i;
for(i=1;i<100;i++)
{
cout<<"\b\b\b"<<i;
cout.flush();
sleep(1);
}
return 0;
}