PDA

View Full Version : مبتدی: استفاده از cin.getline



eamaster
شنبه 28 دی 1392, 17:06 عصر
سلام خدمت اساتید گرامی
اگه ممکنه راهنمایی کنید :افسرده:
برای اینکه در قطعه کد زیر نتعداد زیاد کاراکتر بصورت جمله دریافت کنیم چه کار باید کرد؟
void Season::addSeason()
{
Season* temp = new Season;
Season* temp2 = Start_ptr;
system("cls");
cout << "Enter Season Name: ";
//cin >> temp->nameSeason;
cin.getline(temp->nameSeason, 50, ".");
cout << "Enter text: ";
//cin >> temp->text;
cin.getline(temp->text, 50, ".");
temp->next = NULL;
if (Start_ptr == NULL)
{
Start_ptr = temp;
}
else
{
while (temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
}
من بصورت کد بالا از cin.getline استفاده می کنم ارور میده باید چیکار کنم؟

omidshaman
شنبه 28 دی 1392, 21:17 عصر
اولا که اسم رو اصولا میزارن seasonName نه nameSeason .
بعد این که متن ارور رو همیشه این جور جا ها بزار
شما از cin.getline اشتباه استفاده کردی.
استفادش این جوریه برای مثال :

string a;
cin.getline(a,50);

eamaster
شنبه 28 دی 1392, 23:52 عصر
مرسی از راهنماییتون این ارورش
3 IntelliSense: no instance of overloaded function "std::basic_istream<_Elem, _Traits>::getline [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list
argument types are: (std::string, int)
object type is: std::istream c:\Users\EAMSTER\Desktop\micro\micro\Season.cpp 20 5 micro

چون از لیست پیوندی استفاده شده این کارو کردم

void Season::addSeason()
{
Season* temp = new Season;
Season* temp2 = Start_ptr;
system("cls");
cout << "Enter Season Name: ";
//cin >> temp->nameSeason;
cin.getline(temp->nameSeason, 50);
cout << "Enter text: ";
//cin >> temp->text;
cin.getline(temp->text, 50);
temp->next = NULL;
if (Start_ptr == NULL)
{
Start_ptr = temp;
}
else
{
while (temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
}

اینم کل برنامه باید چیکار کنم :ناراحت:

omidshaman
یک شنبه 29 دی 1392, 09:30 صبح
سلام :
season.cpp

#include <iostream>
#include <string>
#include <fstream>
#include "Season.h"
using namespace std;

Season::Season()
{
Start_ptr = NULL;
current = NULL;
}
//add function
void Season::addSeason()
{
Season* temp = new Season;
Season* temp2 = Start_ptr;
system("cls");
cout << "Enter Season Name: ";
//cin >> temp->nameSeason;
getline(std::cin,temp->nameSeason);
cout << "Enter text: ";
//cin >> temp->text;
getline(std::cin,temp->text);
temp->next = NULL;
if (Start_ptr == NULL)
{
Start_ptr = temp;
}
else
{
while (temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
}
//seach
void Season::searchSeason(){
string name;
int t = 0;
cout << "Enter first name for search:";
cin >> name;
Season *temp = Start_ptr;
while (temp){
if (temp->nameSeason == name){
cout << "Found: " << temp->nameSeason << endl << temp->text;
t = 1;
break;
}
else
temp = temp->next;
}
if (t == 0)
cout << "Don,t Exist.";
}
//delete
void Season::deleteSeason(string &cName){
Season *curp = Start_ptr;
Season *nextp = NULL;
while (curp != NULL && curp->nameSeason != cName){
nextp = curp;
curp = curp->next;
}
if (curp == NULL){
cout << "This Name is not exist.";
return;
}
if (curp == Start_ptr){
Start_ptr = Start_ptr->next;
if (curp == current){
current = NULL;
}
}
else if (curp == current){
current = nextp;
}
else{
nextp->next = curp->next;
}
delete curp;
cout << "delete comlate" << endl;
}
void Season::showAll()
{
Season *temp = Start_ptr;

if (Start_ptr == NULL)
{
cout << "The list is empty!" << endl;
cin.get();
return;
}
else
{
cout<< "\n----------------------------------- ------------" << endl;
do
{
cout << temp->nameSeason << " " << temp->text <<endl;
temp = temp->next;
} while (temp != NULL);
}
cout << endl;
cin.get();
}



تابع Menu1 توی main :

int Menu1()
{
int ch;
system("cls");
cout << " (MENU)" << endl;
cout << "1. Add new Season." << "\n2. Delete a Season." << "\n3. Search Season." << "\n4. Format." << "\n5. Show all list." << "\n6. Save." << "\n8. Exit." << "\n\nYour choice : ";
cin >> ch;
cin.ignore();
return ch;
}

eamaster
یک شنبه 29 دی 1392, 14:30 عصر
خیلی ممنون از راهنماییتون
یک سوال دیگه من وقتی متن و تو text ذخیره می کنم و بعد می خوام نمایش بدم space هارو درنظر نمیگیره و بعد هر کلمه میره خط بعدی !!
باید چیکار کرد که درست نمایش بده:لبخندساده:

فایل رو ضمیمه کردم براتون