نمایش نتایج 1 تا 4 از 4

نام تاپیک: مشکل با gets در حلقه تکرار

  1. #1
    منتظر تایید آدرس ایمیل
    تاریخ عضویت
    بهمن 1387
    محل زندگی
    شیراز
    پست
    178

    Question مشکل با gets در حلقه تکرار

    با سلام
    دوستان کد زیر رو در ویژوال سی پلاس پلاس 2008 نوشتم در حلقه و در بار اول اجرای حلقه تابع gets اجرا میشه و در دفعات بعد سیستم از اون میگذره و رشته ای رو دریافت نمیکنه.
    لطفا دلیل رو شرح دهید
    با تشکر

    \\Created With Microsoft Visual Studio 2008
    #include "stdafx.h"
    #include <conio.h>
    #include <cstring>
    #include <iostream>
    using namespace std;
    struct daneshjo{
    char name[15];
    char famil[15];
    long double sd;
    }Arr[5];

    int main()
    {
    int i;
    cout<<"Enter Data\n";
    for(i=0;i<5;i++)
    {
    cout<<"enter name: ";

    ///Error Here
    gets(Arr[i].name);
    //////////////////
    cout<<"enter family: ";
    gets(Arr[i].famil);

    cout<<"enter shoma d: ";
    cin>>Arr[i].sd;
    }
    return 0;
    }

  2. #2
    کاربر دائمی آواتار clover
    تاریخ عضویت
    خرداد 1388
    محل زندگی
    اصفهان - اراک
    پست
    646

    نقل قول: مشکل با gets در حلقه تکرار

    لطفا دلیل رو شرح دهید
    متاسفانه دلیلش را دقیقا نمی دونم اما بعد از cin، یک کاراکتر 'n\' در جریان ورودی باقی میمونه که باعث پایان یافتن تابع gets بدون ورود رشته میشه.
    برای رفع مشکل این خط را در انتهای حلقه (بعد از cin) اضافه کنید:
    cin.ignore();


    تاپیک مرتبط:

  3. #3
    منتظر تایید آدرس ایمیل
    تاریخ عضویت
    بهمن 1387
    محل زندگی
    شیراز
    پست
    178

    نقل قول: مشکل با gets در حلقه تکرار

    مرسی از پاسختان اما برای حل مشکل هزار و یک راه وجود داره اما من دنبال دلیلش هستم

    به هر حال تشکر

  4. #4
    کاربر دائمی آواتار clover
    تاریخ عضویت
    خرداد 1388
    محل زندگی
    اصفهان - اراک
    پست
    646

    نقل قول: مشکل با gets در حلقه تکرار

    اما من دنبال دلیلش هستم
    این هم دلیل:
    std::cin.getline() can run into problems when used with std::cin >> var.
    getline can be provided a third argument--a "stop" character. This character ends getline's input. The character is eaten and the string is terminated. Example:
    std::cin.getline(str, 100, '|')
    If std::cin.getline() is not provided a "stop" character as a third argument, it will stop when it reaches a newline.
    Given:
    float fl;
    std::cin >> fl;
    char str[101]
    std::cin.getline(str, 101);
    And you type: 3.14<return>
    3.14 is read into fl . The newline following the 3.14 is still sitting on the input buffer.
    std::cin.getline(str, 101) immediately processes the newline that is still on the input buffer. str becomes an empty string.
    The illusion is that the application "skipped" the std::cin.getline() statement.

    The solution is to add std::cin.ignore(); immediately after the first std::cin statement. This will grab a character off of the input buffer (in this case, newline) and discard it.

    std::cin.ignore() can be called three different ways:
    No arguments: A single character is taken from the input buffer and discarded:
    std::cin.ignore(); //discard 1 character
    One argument: The number of characters specified are taken from the input buffer and discarded:
    std::cin.ignore(33); //discard 33 characters
    Two arguments: discard the number of characters specified, or discard characters up to and including the specified delimiter (whichever comes first):
    std::cin.ignore(26, '\n'); //ignore 26 characters or to a newline, whichever comes first
    تقریبا همون چیزی بود که فکر می کردم.
    منبع:

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •