im just a beginner here,i encounted some problems while programming this,when i include a get line i get iso c++ forbits comparisor between pointer and integer.however when i change the name to string it works but the loop encounters a skip.this is my program.please help!!!
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int num1=10;
char name[10];
string date[num1]={""};
double number[num1]={NULL},cost[num1]={NULL};
for(int i=0;i<num1;i++)
{
cout << "Enter name of item(no space use _)(t to terminate):";
cin.getline(name,9);
if(name[i]=="t")
{
name[i]="";
break;
}
cout << "Enter date to be purchased by:";
cin >> date[i];
cout << "Enter amount of item: " ;
cin >> number[i];
cout << "Enter the cost of item:";
cin >> cost[i];
}
cout << "\n\tItem\tdate\t\tamount\tcost of each\n";
for (int i=0; i<10; i++)
{
cout << i+1 << ": \t" << name[i] << "\t" << date[i] << "\t" << number[i] << "\t" << cost[i] << endl;
}
name[i] is a char. "t" is a string literal, converted to cons char*. You are trying to compare a single character to the whole string.
You probably want to compare it to another character: 't'
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int num1=10;
string name[num1]={""},t;
string date[num1]={""};
double number[num1]={NULL},cost[num1]={NULL};
for(int i=0;i<num1;i++)
{
cout << "Enter name of item(t to terminate):";
getline(cin,name[i]);
if(name[i]=="t")
{
name[i]="";
break;
}
cout << "Enter date to be purchased by:";
cin >> date[i];
cout << "Enter amount of item: " ;
cin >> number[i];
cout << "Enter the cost of item:";
cin >> cost[i];
cin.ignore(1024, '\n');
}
cout << "\n\tItem\tdate\t\tamount\tcost of each\n";
for (int i=0; i<10; i++)
{
cout << i+1 << ": \t" << name[i] << "\t" << date[i] << "\t" << number[i] << "\t" << cost[i] << endl;
}
return 0;
}
thanks ,i done what u said and it seems to work. i added cin.ignore(1024, '\n'); and it works fine, i cant seem to find any errors do u think its okay?
Well, yeah, it works, of course. But there's almost always mistakes. :D For example if you don't enter a double for cost[i] and you enter a char or whatever, the program will crash. :P