This is my code!
#include "stdafx.h"
#include<iostream>
#include<conio.h>
void main()
{
clrscr();
int no;
cout<<"Enter a number";
cin>>no;
getch();
}
and I get this error here!
I think I might have to download some extra visual studio c++ related directories, but still some suggestions please
clrscr() is not a standard function. Visual Studio does not have it. However, MSDN does document how to clear the screen using system("cls"), or FillConsoleOutputCharacter() and FillConsoleOutputAttribute().
As for the cin/cout errors, you need to prefix them with the std:: namespace qualifier, eg std::cin and std::cout, or use a separate using namespace std; statement in your code below the header #include statements.
Try this:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <conio.h>
void clrscr()
{
std::system("cls");
}
int main()
{
clrscr();
int no;
std::cout << "Enter a number";
std::cin >> no;
getch();
return 0;
}
Or:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
void clrscr()
{
std::system("cls");
}
int main()
{
clrscr();
int no;
cout << "Enter a number";
cin >> no;
getch();
return 0;
}
Right now your c++ program doesn't know what clrscr(); is..
you have to define that function. To define it, see #Remy Lebeau's answer.
One quick solution instead of creating a function to clear out the screen is to simply cout a bunch of blank spaces.
so in your main you can simply put :
std::cout << string(50, '\n');
Related
In my program it should be an option to ask a user for input, and then save input string into the file. My problem is, - when I put cin in any of it forms, inside Switch, program will stuck circling indefinitely, right after i press enter after finish typing new text. What could cause the problem?
#include <iostream>
#include <fstream>
#include <iterator>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <algorithm>
#include <chrono>
#include <random>
#include <vector>
using namespace std;
void changePlainText()
{
ofstream nFile("plaintext.txt");
string newText;
cout << "Enter new plain text" << endl;
getline(cin, newText);
nFile << newText;
nFile.close();
}
int main()
{
int uInput = 0;
do
{
printf("2.Change content of the plain text file: \n");
cin >> uInput;
switch (uInput)
{
case 1:
break;
case 2:
changePlainText();
break;
}
} while (uInput != 5);
cout << "Closing program" << endl;
system("pause");
}
After I type something in console and press enter, the program enters never ending circle. It still stuck even if I just write simple cin >> i, in switch case.
Take a look at this question. I debugged your code and I experienced that exact behavior. The accepted answer explains quite well what's happening and also provides a solution.
I tried with boost, I changed
cin >> uInput;
to
string inputString; // so you know what inputString is
getline(cin, inputString);
uInput = boost::lexical_cast<int>(line);
and it's working fine now.
In CLion getch() is waiting for pressing of Enter button in this code
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
char c = getch();
cout << c;
}
How can I fix it?
So i got a little beginner problem here.
I cant seem to print out a string with char and integers in it.
#include <iostream.h>
#include <conio.h>
Main()
{
Char date[20];
Clrscr();
Cout<<"enter date: ";
Cin>>date;
Cout<<endl;
Cout<<date;
Getch();
Return 0;
}
My input here is suppose to be :
January 1-5,1999
But all it shows is :
January.**
Use getline(). Otherwise it cuts it after a space. Also, do not use capitals for cout, etc.
Like
string date;
getline(cin,date, '\n');
The answer by #Caspar Wylie is correct but if are using a very old/outdated compiler(guessed from conio.h and iostream.h header files) then try this
#include <iostream.h>
#include <conio.h>
int main()
{
char date[20];
cin.getline(str,20);
cout << date << endl;
getch();
return 0;
}
I'm trying to write a program that gives a table of temperature for each hour in one day with some starts that give an idea about the temperature. My problem is that eclipse (I use it on Mac OS) gives me "method 'open' can not be resolved". Would you please help me solving this problem?
And it would be so great if you have any suggestions in order to better the program.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include<cstring>
using namespace std;
int main()
{char temp[30];
cout<<"what is the name of the file you are using? ";
int rain[24],i,j,stars;
cin>>temp;
ifstream input;
input.open(temp);
if(input.fail())
{ cout<<"file did not open please check it\n";
system("pause");
return 1;
}
for(i=0;i<24;i++)
{input>>rain[i] ;
}
for(i=-30;i<=120;i+=30)
cout<<setw(10)<<right<<i;
cout<<endl;
for(i=0;i<24;i++)
{cout<<setw(10)<<left<<rain[i];
if(rain[i]<0)
{stars=(int)(abs(rain[i]) /3.);
for(j=1;j<10-stars;j++)
cout<<" ";
for(j=0;j< stars;j++)
cout<<"*";
cout<<"|\n";
}
else
{for(j=1;j<10;j++)
cout<<" ";
cout<<"|";
stars=(int)(rain[i]/3.);
for(j=0;j<stars;j++)
cout<<"*";
cout<<endl;
}
}
input.close();
system("pause");
return 0;
}
This should work..
Open Project explorer, right click on your project > Index > Rebuild
I'm doing this little program in C++ but in codeblocks appears the following error: error:
'atod' was not declared in this scope
What I'm missing in this code? Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <fstream>
#include <math.h>
#include <iostream>
using namespace std;
class birth{
};
int main (void){
int pass=1;
string date, boy, aux1, aux2, aux4;
double f;
while(pass=! 0){
cout<<"Enter the name of the birthday boy"<<endl;
cin>>boy;
cout<<"Enter the date of birth" <<endl;
cin>>date;
aux1= aux1.substr(5,10);
f= atod(aux1);
f=2012-f;
cout<< "The birthday boy "<<boy<<"who born"<<date<<"now have"<<f<<"years"<<endl
cout<<"Do you want to enter more birthdays?"<<endl;
cout<<"1.- YES"<<endl;
cout<<"2.- NO"<<endl;
cin>>pass;
}
system ("pause");
return 0;
};
EDIT: The problem is in this line:
f= atod(aux1);
Use the atof function. It's in stdlib.h. You need to pass a const char* to it, not a std::string.
f = atof(aux1.c_str());
there is no any function like atod
use _atold() or atof() , these are in math.h & stdlib.h