C:提示一次输入一行并检查

时间:2018-11-22 05:54:24

标签: c loops printf scanf user-input

问题陈述:
循环5次。每次询问用户一个整数,检查接收到的输入是否是该类型,然后询问另一个输入,直到用户给出了五个正确的输入。假设用户给出了int类型的五个正确输入,则该程序的输出应如下所示:

你好!请给我一个整数:0
谢谢!请再给我一个整数:1
谢谢!请再给我一个整数:1
谢谢!请给我另一个整数:2
谢谢!请给我另一个整数:3
谢谢!我对五个整数感到满意。

我的尝试:

Downloading DB2 ODBC CLI Driver from https://public.dhe.ibm.com/ibmdl/export/pub/software/data/db2/drivers/odbc_cli/linuxx64_odbc_cli.tar.gz...

100.00% | 21040638 bytes downloaded out of 21040638 bytes.

****************************************
You are downloading a package which includes the Node.js module for IBM DB2/Informix.  The module is licensed under the Apache License 2.0. The package also includes IBM ODBC and CLI Driver from IBM, which is automatically downloaded as the node module is installed on your system/device. The license agreement to the IBM ODBC and CLI Driver is available in undefined   Check for additional dependencies, which may come with their own license agreement(s). Your use of the components of the package and dependencies constitutes your acceptance of their respective license agreements. If you do not accept the terms of any licenseagreement(s), then delete the relevant component(s) from your device.
****************************************

Downloading and extraction of DB2 ODBC CLI Driver completed successfully ...

make: Entering directory '/home/rahukkalidindi/Code/CognitiveSpacesPortal/node_modules/ibm_db/build'
  CXX(target) Release/obj.target/odbc_bindings/src/odbc.o
odbc_bindings.target.mk:108: recipe for target 'Release/obj.target/odbc_bindings/src/odbc.o' failed
make: Leaving directory '/home/rahukkalidindi/Code/CognitiveSpacesPortal/node_modules/ibm_db/build'

{ Error: Command failed: node-gyp configure build  --IS_DOWNLOADED=true --IBM_DB_HOME="$IBM_DB_HOME"
In file included from ../src/odbc.h:23:0,
                 from ../src/odbc.cpp:28:
../../nan/nan.h: In constructor ‘Nan::Utf8String::Utf8String(v8::Local<v8::Value>)’:
../../nan/nan.h:1064:78: warning: ‘v8::Local<v8::String> v8::Value::ToString(v8::Isolate*) const’ is deprecated: Use maybe version [-Wdeprecated-declarations]
       v8::Local<v8::String> string = from->ToString(v8::Isolate::GetCurrent());
                                                                              ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2538:31: note: declared here
                 Local<String> ToString(Isolate* isolate) const);
                               ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp: In static member function ‘static void ODBC::UV_AfterCreateConnection(uv_work_t*, int)’:
../src/odbc.cpp:210:27: warning: ‘v8::Local<v8::Value> Nan::Callback::Call(int, v8::Local<v8::Value>*) const’ is deprecated [-Wdeprecated-declarations]
     data->cb->Call(1, info);
                           ^
In file included from ../src/odbc.h:23:0,
                 from ../src/odbc.cpp:28:
../../nan/nan.h:1655:3: note: declared here
   Call(int argc, v8::Local<v8::Value> argv[]) const {
   ^~~~
../src/odbc.cpp:217:99: error: no matching function for call to ‘v8::Function::NewInstance(int, v8::Local<v8::Value> [2])’
     Local<Object> js_result = Nan::New<Function>(ODBCConnection::constructor)->NewInstance(2, info);
                                                                                                   ^
In file included from ../src/odbc.cpp:22:0:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:4099:44: note: candidate: v8::MaybeLocal<v8::Object> v8::Function::NewInstance(v8::Local<v8::Context>, int, v8::Local<v8::Value>*) const
   V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
                                            ^~~~~~~~~~~
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:4099:44: note:   candidate expects 3 arguments, 2 provided
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:4102:44: note: candidate: v8::MaybeLocal<v8::Object> v8::Function::NewInstance(v8::Local<v8::Context>) const
   V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
                                            ^~~~~~~~~~~
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:4102:44: note:   candidate expects 1 argument, 2 provided
../src/odbc.cpp:222:27: warning: ‘v8::Local<v8::Value> Nan::Callback::Call(int, v8::Local<v8::Value>*) const’ is deprecated [-Wdeprecated-declarations]
     data->cb->Call(2, info);
                           ^
In file included from ../src/odbc.h:23:0,
                 from ../src/odbc.cpp:28:
../../nan/nan.h:1655:3: note: declared here
   Call(int argc, v8::Local<v8::Value> argv[]) const {
   ^~~~
../src/odbc.cpp: In static member function ‘static Nan::NAN_METHOD_RETURN_TYPE ODBC::CreateConnectionSync(Nan::NAN_METHOD_ARGS_TYPE)’:
../src/odbc.cpp:262:99: error: no matching function for call to ‘v8::Function::NewInstance(int, v8::Local<v8::Value> [2])’
   Local<Object> js_result = Nan::New<Function>(ODBCConnection::constructor)->NewInstance(2, params);
                                                                                                   ^
In file included from ../src/odbc.cpp:22:0:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:4099:44: note: candidate: v8::MaybeLocal<v8::Object> v8::Function::NewInstance(v8::Local<v8::Context>, int, v8::Local<v8::Value>*) const
   V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
                                            ^~~~~~~~~~~
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:4099:44: note:   candidate expects 3 arguments, 2 provided
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:4102:44: note: candidate: v8::MaybeLocal<v8::Object> v8::Function::NewInstance(v8::Local<v8::Context>) const
   V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
                                            ^~~~~~~~~~~
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:4102:44: note:   candidate expects 1 argument, 2 provided
../src/odbc.cpp: In static member function ‘static v8::Handle<v8::Value> ODBC::GetColumnValue(SQLHSTMT, Column, uint16_t*, int)’:
../src/odbc.cpp:671:78: warning: format ‘%i’ expects argument of type ‘int’, but argument 4 has type ‘long int’ [-Wformat=]
                   ", columnType = %i, index = %i\n", ret, (long)(hStmt) >> 16 & 0x0000ffff,
                                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                   (long)(hStmt) & 0x0000ffff, (int) column.type, column.index);
                                                                              ^
../src/odbc.cpp:671:78: warning: format ‘%i’ expects argument of type ‘int’, but argument 5 has type ‘long int’ [-Wformat=]
../src/odbc.cpp: In static member function ‘static Parameter* ODBC::GetParametersFromArray(v8::Local<v8::Array>, int*)’:
../src/odbc.cpp:912:51: warning: ‘int64_t v8::Value::IntegerValue() const’ is deprecated: Use maybe version [-Wdeprecated-declarations]
           params[i].paramtype = val->IntegerValue();
                                                   ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2570:46: note: declared here
   V8_DEPRECATED("Use maybe version", int64_t IntegerValue() const);
                                              ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp:916:48: warning: ‘int64_t v8::Value::IntegerValue() const’ is deprecated: Use maybe version [-Wdeprecated-declarations]
           params[i].c_type = val->IntegerValue();
                                                ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2570:46: note: declared here
   V8_DEPRECATED("Use maybe version", int64_t IntegerValue() const);
                                              ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp:922:46: warning: ‘int64_t v8::Value::IntegerValue() const’ is deprecated: Use maybe version [-Wdeprecated-declarations]
           params[i].type = val->IntegerValue();
                                              ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2570:46: note: declared here
   V8_DEPRECATED("Use maybe version", int64_t IntegerValue() const);
                                              ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp:930:59: warning: ‘int64_t v8::Value::IntegerValue() const’ is deprecated: Use maybe version [-Wdeprecated-declarations]
               params[i].buffer_length = val->IntegerValue();
                                                           ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2570:46: note: declared here
   V8_DEPRECATED("Use maybe version", int64_t IntegerValue() const);
                                              ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp: In static member function ‘static void ODBC::GetStringParam(v8::Local<v8::Value>, Parameter*, int)’:
../src/odbc.cpp:972:44: warning: ‘v8::Local<v8::String> v8::Value::ToString() const’ is deprecated: Use maybe version [-Wdeprecated-declarations]
     Local<String> string = value->ToString();
                                            ^
In file included from ../src/odbc.cpp:22:0:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:10242:15: note: declared here
 Local<String> Value::ToString() const {
               ^~~~~
../src/odbc.cpp:988:38: warning: ‘int v8::String::Utf8Length() const’ is deprecated: Use Isolate version instead [-Wdeprecated-declarations]
         bufflen = string->Utf8Length() + 1;
                                      ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2682:52: note: declared here
   V8_DEPRECATED("Use Isolate version instead", int Utf8Length() const);
                                                    ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp:1008:49: warning: ‘int v8::String::WriteUtf8(char*, int, int*, int) const’ is deprecated: Use Isolate* version [-Wdeprecated-declarations]
         string->WriteUtf8((char *) param->buffer);
                                                 ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2754:21: note: declared here
                 int WriteUtf8(char* buffer, int length = -1,
                     ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp:1015:54: warning: ‘int v8::String::WriteOneByte(uint8_t*, int, int, int) const’ is deprecated: Use Isolate* version [-Wdeprecated-declarations]
         string->WriteOneByte((uint8_t *)param->buffer);
                                                      ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2747:21: note: declared here
                 int WriteOneByte(uint8_t* buffer, int start = 0,
                     ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp:1023:49: warning: ‘int v8::String::WriteUtf8(char*, int, int*, int) const’ is deprecated: Use Isolate* version [-Wdeprecated-declarations]
         string->WriteUtf8((char *) param->buffer);
                                                 ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2754:21: note: declared here
                 int WriteUtf8(char* buffer, int length = -1,
                     ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp: In static member function ‘static void ODBC::GetInt32Param(v8::Local<v8::Value>, Parameter*, int)’:
../src/odbc.cpp:1066:56: warning: ‘int64_t v8::Value::IntegerValue() const’ is deprecated: Use maybe version [-Wdeprecated-declarations]
     int64_t  *number = new int64_t(value->IntegerValue());
                                                        ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2570:46: note: declared here
   V8_DEPRECATED("Use maybe version", int64_t IntegerValue() const);
                                              ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp: In static member function ‘static void ODBC::GetNumberParam(v8::Local<v8::Value>, Parameter*, int)’:
../src/odbc.cpp:1082:54: warning: ‘double v8::Value::NumberValue() const’ is deprecated: Use maybe version [-Wdeprecated-declarations]
     double *number   = new double(value->NumberValue());
                                                      ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2569:45: note: declared here
   V8_DEPRECATED("Use maybe version", double NumberValue() const);
                                             ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp: In static member function ‘static void ODBC::GetBoolParam(v8::Local<v8::Value>, Parameter*, int)’:
../src/odbc.cpp:1103:53: warning: ‘bool v8::Value::BooleanValue() const’ is deprecated: Use maybe version [-Wdeprecated-declarations]
     bool *boolean    = new bool(value->BooleanValue());
                                                     ^
In file included from /home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:26:0,
                 from ../src/odbc.cpp:22:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:2568:43: note: declared here
   V8_DEPRECATED("Use maybe version", bool BooleanValue() const);
                                           ^
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8config.h:326:3: note: in definition of macro ‘V8_DEPRECATED’
   declarator __attribute__((deprecated(message)))
   ^~~~~~~~~~
../src/odbc.cpp: In static member function ‘static v8::Local<v8::Value> ODBC::CallbackSQLError(SQLSMALLINT, SQLHANDLE, char*,Nan::Callback*)’:
../src/odbc.cpp:1194:19: warning: ‘v8::Local<v8::Value> Nan::Callback::Call(int, v8::Local<v8::Value>*) const’ is deprecated [-Wdeprecated-declarations]
   cb->Call(1, info);
                   ^
In file included from ../src/odbc.h:23:0,
                 from ../src/odbc.cpp:28:
../../nan/nan.h:1655:3: note: declared here
   Call(int argc, v8::Local<v8::Value> argv[]) const {
   ^~~~
../src/odbc.cpp: In static member function ‘static v8::Local<v8::Value> ODBC::GetSQLError(SQLSMALLINT, SQLHANDLE, char*)’:
../src/odbc.cpp:1258:87: error: no matching function for call to ‘v8::Object::SetPrototype(v8::Local<v8::Value>)’
       objError->SetPrototype(Exception::Error(Nan::New(errorMessage).ToLocalChecked()));
                                                                                       ^
In file included from ../src/odbc.cpp:22:0:
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:3548:37: note: candidate: v8::Maybe<bool> v8::Object::SetPrototype(v8::Local<v8::Context>, v8::Local<v8::Value>)
   V8_WARN_UNUSED_RESULT Maybe<bool> SetPrototype(Local<Context> context,
                                     ^~~~~~~~~~~~
/home/rahukkalidindi/.node-gyp/11.0.0/include/node/v8.h:3548:37: note:   candidate expects 2 arguments, 1 provided
../src/odbc.cpp: In static member function ‘static v8::Handle<v8::Value> ODBC::GetColumnValue(SQLHSTMT, Column, uint16_t*, int)’:
../src/odbc.cpp:562:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
         if((int) column.type == SQL_NUMERIC)
         ^~
../src/odbc.cpp:564:5: note: here
     case SQL_DECIMAL :
     ^~~~
../src/odbc.cpp:565:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
         if((int) column.type == SQL_DECIMAL)
         ^~
../src/odbc.cpp:567:5: note: here
     case SQL_BIGINT :
     ^~~~
../src/odbc.cpp:568:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
         if((int) column.type == SQL_BIGINT)
         ^~
../src/odbc.cpp:570:5: note: here
     case SQL_DBCLOB:
     ^~~~
../src/odbc.cpp:571:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
         if((int) column.type == SQL_DBCLOB)
         ^~
../src/odbc.cpp:577:5: note: here
     case SQL_BLOB :
     ^~~~
../src/odbc.cpp:578:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
         if((int) column.type == SQL_BLOB)
         ^~
../src/odbc.cpp:584:5: note: here
     default :
     ^~~~~~~
../src/odbc.cpp: In static member function ‘static v8::Handle<v8::Value> ODBC::GetOutputParameter(Parameter)’:
../src/odbc.cpp:699:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
         if((int) prm.type == SQL_NUMERIC)
         ^~
../src/odbc.cpp:701:5: note: here
     case SQL_BIGINT :
     ^~~~
../src/odbc.cpp:745:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
         if((int) prm.type == SQL_DECIMAL)
         ^~
../src/odbc.cpp:747:5: note: here
     case SQL_FLOAT :
     ^~~~
../src/odbc.cpp:790:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
         if((int) prm.type == SQL_DBCLOB)
         ^~
../src/odbc.cpp:794:5: note: here
     case SQL_BLOB :
     ^~~~
../src/odbc.cpp:795:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
         if((int) prm.type == SQL_BLOB)
         ^~
../src/odbc.cpp:799:5: note: here
     default :
     ^~~~~~~
make: *** [Release/obj.target/odbc_bindings/src/odbc.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/home/linuxbrew/.linuxbrew/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:262:23)
gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
gyp ERR! System Linux 4.15.0-38-generic
gyp ERR! command "/home/linuxbrew/.linuxbrew/Cellar/node/11.0.0/bin/node" "/home/linuxbrew/.linuxbrew/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "build" "--IS_DOWNLOADED=true" "--IBM_DB_HOME=/home/rahukkalidindi/Code/CognitiveSpacesPortal/node_modules/ibm_db/installer/clidriver"
gyp ERR! cwd /home/rahukkalidindi/Code/CognitiveSpacesPortal/node_modules/ibm_db
gyp ERR! node -v v11.0.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok

    at ChildProcess.exithandler (child_process.js:289:12)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:962:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
  killed: false,
  code: 1,
  signal: null,
  cmd:
   'node-gyp configure build  --IS_DOWNLOADED=true --IBM_DB_HOME="$IBM_DB_HOME"' }
npm WARN The package gulp-sass is included as both a dev and production dependency.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ibm_db@2.4.1 install: `node installer/driverInstall.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ibm_db@2.4.1 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我从第一个输入获得的常见输出是字母或非整数:

#include <stdio.h>

int invalid(x)  
{  
    printf("\nThat was not an integer, please give me an integer: ");  
    scanf("%d", &x);
}

int main()
{
    int a, b, c, d, e, x;

    printf("Hello! Please give me an integer: ");
    scanf("%d", &a);
            if(scanf("%d", &a) != 1)
            {
                    invalid(x);
            }
    printf("\nThanks! Please give me another integer: ");
    scanf("%d", &b);
            if(scanf("%d", &b) != 1)
            {
                    invalid(x);
            }
    printf("\nThanks! Please give me another integer: ");
    scanf("%d", &c);
            if(scanf("%d", &c) != 1)
            {
                    invalid(x);
            }
    printf("\nThanks! Please give me another integer: ");
    scanf("%d", &d);
            if(scanf("%d", &d) != 1)
            {
                    invalid(x);
            }
    printf("\nThanks! Please give me another integer: ");
    scanf("%d", &e);
            if(scanf("%d", &e) != 1)
            {
                    invalid(x);
            }
    printf("\nThanks! I am happy with five integers.\n");
    return 0;
}


// Failed attempt to use a loop //
for(i = 0; i < 4; i++)
{
     printf("Thanks! Please give me another integer: ");
     scanf("%d", &y);
              if(scanf("%d", &y) != 1)
              {
                     invalid(y);
              }
 }







do
{
     printf ("Thanks! Please give me another integer: ", );
     scanf("%d", &x);
         for(scanf("%d", &x) != 1)
         {
              printf("That was not an integer, please give me an integer: ")
              scanf("%d", &x);
         }
         i++;
} while (i < 4);

1 个答案:

答案 0 :(得分:1)

  1. 您必须使用数组来存储5个数字(或4个或大于1的任何其他数量)
  2. 无论如何,您都应该使用循环来问5(或其他一系列数字),这里for循环会很好
  3. 您应该分析scanf返回的值,以检查输入是否正确,然后从中删除不正确的字符后再询问值是否错误(这也可以是循环的,但是do..while是可更改的)输入缓冲区。

更新

我的编版本:

#include <stdio.h>

#define NUM_CNT 5

int main(void)
{
    int i, res, c;
    int num[NUM_CNT]; // array for all your numbers
    printf("Hello!\n");
    for (i = 0; i < NUM_CNT; i++) {
        printf("Please give me an integer: ");
        do {
            res = scanf("%d", &num[i]);
            if ( res ) {
                printf("Thanks!\n");
            } else {
                printf("That was not an integer, please give me an integer: ");
                while ((c = getchar()) != '\n' && c != EOF); // clean input buffer
            }
        } while(res != 1);
    }
    printf("I am happy with five integers.\n");
    // just to see all the numbers
    for (i = 0; i < NUM_CNT; i++) {
        printf("%d ", num[i]);
    }
    return 0;
}

您可以根据我的想法随意修改代码。