What I need to declare in this scope? - c++

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

Related

How to assign char to string object in c++?

i am trying to convert the string into capital letter string by assigning single char's to string like this:-
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string a;
getline(cin,a);
string b;
b.reserve(a.size()+1);
for(int i=(a.size()),i1=0;1;i1++)
{
if(b[i1]!='\0')
b[i1]=(char)toupper(a[i1]);
else
{
a[i1]='\0';
break;
}
}
cout << b <<endl;
}
every when run a.out by ./a.out ,Only endl gets prints
here is sample run:-
$ ./a.out
play clash royale
$
What is wrong in my program?? How can I assign single char to string??
There are some issues with your program. The main one is probably the diference between string reserve and string resize. What you want in your program is already had a string of a.size() length, so, use b.resize(a.size()).
A working version is bellow (there are better ways to write this, just being most consistent with OP proposal):
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string a;
getline(cin,a);
string b;
b.resize(a.size());
for(int i1=0; i1 < a.size();i1++)
{
if(a[i1]!='\0')
b[i1]=(char)toupper(a[i1]);
else
{
b[i1]='\0';
break;
}
}
cout << b <<endl;
}

Printing date by string

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;
}

Visual Studio C++, Identifier not found

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');

Ask user to enter his/her name, and then reverse the given name and show it on screen. using character array

My code is this But i am getting garbage values. how do i make it general so that anyone can use it?
#include <iostream>
#include <string>
using namespace std;
void main()
{
char ch[31];
cin.get(ch,31);
for (int i = 30; i >= 0; i--)
{
cout << ch[i];
}
system("pause");
}
You are taking garbage char from you empty cells.
You must first know the number of char inserted by the user
use reverse function from algorithm header file and use string instead of char.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s;
getline(cin,s);
reverse(s.begin(),s.end());
cout<<s<<endl;
return 0;
}

Error] ISO C++ forbids comparison between pointer and integer [-fpermissive]

I'm a beginner in c++ programming and I have this activity in school. I keep getting [Error] ISO C++ forbids comparison between pointer and integer [-fpermissive] in line 15. How do you solve this? Thanks!
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
int pass[5];
int x;
main()
{
cout<<"\nEnter pin code: ";
for(x=0;x<=4;x++)
{
pass[x]=getch();
putch('#');
}
if(pass==86222)
cout<<"\nW E L C O M E!";
else
cout<<"\nIncorrect Pin Code";
getch();
}
You are doing things in a very strange way. If you wanna compare ints. Take and int, read it and compare, Why is the array needed?
The best and simple way to do this is to use only ints.
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
main()
{
int pass;
cout<<"\nEnter pin code: ";
cin>>pass;
if(pass==86222)
cout<<"\nW E L C O M E!";
else
cout<<"\nIncorrect Pin Code";
getch();
}
If you want to do it the way you want, then use strcmp()
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
char pass[5];
int x;
main()
{
cout<<"\nEnter pin code: ";
for(x=0;x<=4;x++)
{
pass[x]=getch();
putch('#');
}
if(!strcmp(pass, "86222"))
cout<<"\nW E L C O M E!";
else
cout<<"\nIncorrect Pin Code";
getch();
}
You are reading characters and comparing them as ints. That won't work....
The following first places the charactes into an array of characters, then converts that aray to an int and compares the int:
char passch[6];
int pass, x;
main()
{
cout<<"\nEnter pin code: ";
for(x=0;x<=4;x++)
{
passch[x]=getch();
putch('#');
}
passch[5]= '\0';
pass= atoi(passch);
if(pass==86222)
cout<<"\nW E L C O M E!";
else
cout<<"\nIncorrect Pin Code";
getch();
}
pass is an array (which is implemented in c++ as a pointer) and 86222 is an integer. you cannot compare those.
As #haris said in their comment, you really just want to store the input as an integer. you do this with std::cin >> pass. then you can compare pass with your stored value.