开发者

How to resolve the issue "No source available for "msvcrt!fgets() " "

开发者 https://www.devze.com 2023-04-03 08:32 出处:网络
Code #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> using na开发者_如何学Cmespace std;

Code

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using na开发者_如何学Cmespace std;
class Chkinval
        {
         char  *inval;
         char *inarray;
         public:
         void inPutProcessor();

        };


void Chkinval::inPutProcessor()
{
     cout << "Input" ;
     fgets(inval,1000,stdin);
     int tmp = atoi(inval);

     for(int lpcnt=0;lpcnt< tmp;lpcnt++)
     {
           fgets(inarray,1000,stdin);
     }
     for(int lpcnt=0;lpcnt< tmp;lpcnt++)
     {
         cout << "The array elements" << inarray[lpcnt];
     }
    }

int main()
{

    Chkinval tmp1 ;
    tmp1.inPutProcessor();

    return 0;
    }

Issue :

The program compiles fine but no result in the console

In the debug mode

i am getting the error message "No source available for "msvcrt!fgets() ""

Is it a operating system issue or do i need to install any library ?


The problem with the code as it is now is that neither inval nor inarray are initialized and you're thus reading up to 1000 bytes into some arbitrary memory location.

 for(int lpcnt=0;lpcnt< tmp;lpcnt++)
 {
       fgets(inarray,1000,stdin);
 }

Is probably also not what you want (even if inarray was initialized) since it'll overwrite the contents on each iteration.

 fgets(inval,1000,stdin);
 int tmp = atoi(inval);

Is not wrong per se, but you're probably better off using fscanf(stdin, "%d", &tmp) (if you were coding C, read on).

A lot of these problems stem from the fact that your code is very C-like. There is no reason (unless it's for homework) to manage that many allocations on your own in C++. Here is a small example that shows a more C++-way of doing some of the things:

#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::cout << "Number of elements to read? " << std::flush;

    // How many lines should we read?
    int count;
    if (!(std::cin >> count) || count <= 0) {
        std::cout << "Invalid element count" << std::endl;
        return 1;
    }


    std::string line;
    std::vector<std::string> lines;

    // Read until EOF (to get newline from above reading)
    if (!std::getline(std::cin, line)) {
        std::cout << "Error reading line" << std::endl;
        return 1;
    }

    // Read lines one at a time adding them to the 'lines' vector
    for (int i = 0; i < count; i++) {
        if (!std::getline(std::cin, line)) {
            std::cout << "Error reading line" << std::endl;
            return 1;    
        }
        lines.push_back(line);
    }

    // Echo the lines back
    for (std::vector<std::string>::const_iterator line_iterator = lines.begin(); line_iterator != lines.end(); ++line_iterator) {
        std::cout << *line_iterator << '\n';
    }
    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号