URGENT!!!problem with getline loop

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

return 0;
}



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'
sorry i dont really understand,however this is my original program, i just dont know how to include a space in the name of the item.


#include <iostream>
#include <string>
using namespace std;

int main()
{
const int num1=10;
string name[num1]={""};
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 >>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];
}
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;
}

i just dont know how to include a space in the name of the item.
If you want to enter name containing spaces, you need to use std::getline:
1
2
3
4
std::string name;
std::cout << "Enter your full name: ";
std::getline(std::cin, name);
std::cout << "Your name is " << name;
Enter your full name: John Smith
Your name is John Smith
Also predicting your next problem: http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction
#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
Last edited on
Thanks for ur help. Really appreciate it :)
Topic archived. No new replies allowed.