You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++函数编写报错求助:expected unqualified-id before ‘{’ token

Fixing "expected unqualified-id before ‘{’ token" Error in Your C++ Admission Function

Hey there! Let's walk through fixing your error and getting your admission function working as intended.

First: What Does That Error Mean?

The expected unqualified-id before ‘{’ token error pops up when the compiler expects a valid identifier (like a function name, variable name, or type) but encounters something unexpected. In your case, the root cause is simple:

Look at your function line:

void admission(double gpa, int score); 

That trailing semicolon tells the compiler "this is the end of the function declaration". So when it hits the { on the next line, it has no idea what function this code block belongs to—hence the error, because it was expecting a function name (or other identifier) before that opening brace.

Other Issues in Your Code

Beyond that initial error, there are a few more things that will break your logic once you fix the first problem:

  • Trailing semicolons on every if/else if: Each if (condition); ends the conditional statement immediately. The code blocks in {} that follow will run no matter what the condition evaluates to, and your else clauses will throw errors because they won't be attached to any valid if.
  • Missing required headers: You're using cout (from <iostream>) and exit()/EXIT_FAILURE (from <cstdlib>) but haven't included these headers.
  • Unqualified standard library calls: cout, exit, and EXIT_FAILURE are part of the std namespace, so you need to prefix them with std:: or add a using namespace std; line (the former is generally better practice to avoid namespace conflicts).

Corrected Code

Here's your code with all fixes applied:

#include <iostream>
#include <cstdlib>

void admission(double gpa, int score) 
{ 
    if (gpa < 0.0 || gpa > 4.0) 
    { 
        std::cout << "GPA must be between 0.0 and 4.0.\n"; 
        std::exit(EXIT_FAILURE); 
    } 
    else if (score < 0 || score > 100) 
    { 
        std::cout << "Test Score must be between 0 and 100.\n"; 
        std::exit(EXIT_FAILURE); 
    } 
    else if (gpa >= 3.6 && score >= 60) 
    { 
        std::cout << "The admission result is: Accepted!\n"; 
    } 
    else if (gpa >= 3.0 && score >= 70) 
    { 
        std::cout << "The admission result is: Accepted!\n"; 
    } 
    else if (gpa >= 2.6 && score >= 80) 
    { 
        std::cout << "The admission result is: Accepted!\n"; 
    } 
    else if (gpa >= 2.0 && score >= 90) 
    { 
        std::cout << "The admission result is: Accepted!\n"; 
    } 
    else 
    { 
        std::cout << "The admission result is: Rejected.\n"; 
    } 
}

Quick Notes on the Fixes:

  1. Removed the trailing semicolon from the function definition line so the { correctly starts the function body.
  2. Removed all semicolons after if/else if conditions, so the code blocks only run when the condition is true.
  3. Added <iostream> and <cstdlib> headers so the compiler recognizes cout and exit().
  4. Prefixed standard library calls with std:: to resolve namespace issues.
  5. Added newline characters (\n) to make output cleaner.

A Quick Suggestion

You mentioned wanting the function to "get user input"—right now, this function takes gpa and score as parameters. If you want it to read input directly from the user, you could modify it to remove the parameters and use std::cin inside the function instead, like this:

#include <iostream>
#include <cstdlib>

void admission() 
{ 
    double gpa;
    int score;

    std::cout << "Enter your GPA: ";
    std::cin >> gpa;
    std::cout << "Enter your test score: ";
    std::cin >> score;

    // Rest of the validation and logic goes here...
}

内容的提问来源于stack exchange,提问作者Alex Zimmerman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:28:24