PDA

View Full Version : سوال: اشکال در کد



hafez1
جمعه 05 آبان 1391, 12:51 عصر
یه برنامه هست که درباره strtok گرفتم از اینترنت.بد خودم یکم تغییرش دادم.به برنامه من ارور می گیره.
این برنامه ایه که از اینترنت گرفتم:



#include <iostream.h>

#include<conio.h>

#include <string.h> // prototype for strtok

int main()

{ clrscr();

char sentence[] = "This is a sentence with 7 tokens";

char *tokenPtr;

cout << "The string to be tokenized is:\n" << sentence

<< "\n\nThe tokens are:\n\n";

// begin tokenization of sentence

tokenPtr = strtok( sentence, " " );

// continue tokenizing sentence until tokenPtr becomes NULL

while ( tokenPtr != NULL )

{

cout << tokenPtr<<”\n”;

tokenPtr = strtok( NULL, “ ” ); // get next token

} // end while

cout << "\nAfter strtok, sentence = " << sentence << endl;

getch();

return 0; // indicates successful termination

} // end main


توی این برنامه رشته ای که اعضاشو جدا می کنه یه رشته ثابته من می خام رشتم دلخواه باشه ینی خودم وارد کنم.
من این جوری برناممو نوشتم:


#include <iostream> #include<conio.h> #include <string.h> // prototype for strtok using namespace std; int main() { string s; cin>>s; char sentence[] = s; char *tokenPtr; cout << "The string to be tokenized is:\n" << sentence << "\n\nThe tokens are:\n\n"; tokenPtr = strtok( sentence, "+" ); while ( tokenPtr != NULL ) { cout << tokenPtr<<"\n"; tokenPtr = strtok( NULL, "+" ); } getch(); return 0; }

اشکالش چیه؟؟؟؟

مسعود اقدسی فام
جمعه 05 آبان 1391, 15:39 عصر
دوست خوبم

شما اینهمه اینجا سوال می‌پرسید و جواب می‌دید. ولی هنوز همچین سوالایی هستن که یا سوال نامفهومهُ یا کد. من این کد رو الان چطور بخونم؟

hafez1
جمعه 05 آبان 1391, 21:02 عصر
بله ببخشید .این کدیه که از نت گرفتم:


#include <iostream>
#include<conio.h>
#include <string.h>
using namespace std;
int main()
{
char sentence[] = "This is a sentence with 7 tokens";
char *tokenPtr;
cout << "The string to be tokenized is:\n" << sentence << "\n\nThe tokens are:\n\n";

tokenPtr = strtok( sentence, " " );

while ( tokenPtr != NULL )
{
cout << tokenPtr<<endl;
tokenPtr = strtok( NULL, " " );

}

cout << "\nAfter strtok, sentence = " << sentence << endl;
getch();
return 0;
}

و اینم کد خودم که ارور داره:


#include <iostream>
#include<conio.h>
#include <string.h> // prototype for strtok
using namespace std;

int main()
{
string s;
cin>>s;
char sentence[] = s;
char *tokenPtr;

cout << "The string to be tokenized is:\n" << sentence
<< "\n\nThe tokens are:\n\n";


tokenPtr = strtok( sentence, "+" );


while ( tokenPtr != NULL )
{
cout << tokenPtr<<"\n";




tokenPtr = strtok( NULL, "+" );
}


getch();
return 0;
}

مسعود اقدسی فام
شنبه 06 آبان 1391, 00:16 صبح
این برنامه با + تفکیک می کنه. اینی که شما نوشتی. آره؟ نیت همینه؟

hafez1
شنبه 06 آبان 1391, 05:33 صبح
بله نیت همینه ولی ارور داره.

مسعود اقدسی فام
شنبه 06 آبان 1391, 05:52 صبح
بررسی کن ببین وقتی s داخل sentence ریخته می شه به آخرش NULL یا همون 0\ اضافه می‌شه؟

مسعود اقدسی فام
شنبه 06 آبان 1391, 06:05 صبح
بله نیت همینه ولی ارور داره.

راستی ارور یعنی برنامه اصلا اجرا نمی‌شه. برنامه اجرا نمی‌شه؟ اگه اجرا می‌شه و جواب درستی نمی‌ده اسمش ارور نیست. خروجی اشتباهه.

اگه خروجی اشتباهه اون چیزی که گفتم تست کنید.

hafez1
شنبه 06 آبان 1391, 19:42 عصر
برنامه اصلا اجرا نمی شه.

مسعود اقدسی فام
شنبه 06 آبان 1391, 20:51 عصر
خب لطف می‌کنی پیام خطا رو بنویسی؟

omidshaman
یک شنبه 07 آبان 1391, 10:59 صبح
مشکل کد شما اونزقسمتیه که استرینگ رو مساوی چار گذاشته بودی
درستش میشه این

#include <iostream>
#include<conio.h>
#include <string.h> // prototype for strtok
using namespace std;

int main()
{
string s;
cin>>s;

char *sentence = (char*)s.c_str();

char *tokenPtr;
cout << "The string to be tokenized is:\n" << sentence
<< "\n\nThe tokens are:\n\n";


tokenPtr = strtok( sentence, "+" );


while ( tokenPtr != NULL )
{
cout << tokenPtr<<"\n";




tokenPtr = strtok( NULL, "+" );
}


getch();
return 0;
}

hafez1
یک شنبه 07 آبان 1391, 19:18 عصر
این برنامه ای که شما نوشتید هم این ارور رو داره:
Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\acer\documents\visual studio 2008\projects\strtok6\strtok6\strtok6.cpp 9

hafez1
یک شنبه 07 آبان 1391, 19:29 عصر
ارور مال string.h هست.وقتی .h رو برداشتم درست شد.خییییییییییییییییییییی یلی ممنون.