简体   繁体   English

具有指针的C ++程序不起作用

[英]C++ Program with pointers not working

I am a beginner and am stuck. 我是一个初学者,被困住了。 I have written this and so far it is not working. 我已经写了这篇文章,到目前为止还行不通。 After "Add or Remove Trader" it does nothing. 在“添加或删除交易者”之后,它什么也不做。 Any help or tidbits on how to make this functional would be greatly appreciated. 任何帮助或花样如何使这一功能将不胜感激。 Thank you. 谢谢。

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

struct Department{
string deptName;
int numTraders;     
};

void addTraders(Department *, int );
void removeTraders(Department *, int);

int main(){

char addOrRemove;
Department departments[10] = {
    {"Bank Loan", 10},
    {"Conservative Allocation", 9},
    {"Europe Stock", 10},
    {"Domestic", 21},
    {"Asia", 10},
    {"Large Growth", 5},
    {"Long-term Bond", 5},
    {"Money Market", 25},
    {"Emerging Market", 18},
    {"Large Blend", 12}
};

int choice, numberToAdd, numberToRemove;

Department* p_departments = departments;

for(int i = 0; i < 10; i++){
    cout << "Department # " << (i + 1) << ", Name: " << p_departments[i].deptName <<
        ", Traders: " << p_departments[i].numTraders << endl;
}
cout << endl;

do{

cout << "Enter 0 to quit, or choose a department number: ";
cin >> choice;

cout << "Add or remove traders (A or R) ? ";
cin >> addOrRemove;

if(addOrRemove == 'A' || 'a'){
    cout << "how many traders to add" << endl;
    cin >> numberToAdd;
    addTraders(&departments[choice-1] ,numberToAdd);
}
else if(addOrRemove == 'R' || 'r'){
    cout << "how many traders to remove" << endl;
    cin >> numberToRemove;
    removeTraders(&departments[choice-1],numberToRemove);
}
else{
    cout << addOrRemove << " is not a valid selection. \n";
}

for(int i = 0; i < 10; i++){
    cout << "Department # " << (i + 1) << ", Name: " << p_departments[i].deptName <<
        ", Traders: " << p_departments[i].numTraders << endl;
}
cout << endl;

}while(count != 0);

system("pause");
return 0;
}

void addTraders(Department *dept, int numAdd){

dept->numTraders += numAdd;
}

void removeTraders(Department *dept, int numRemove){

dept->numTraders += numRemove;
} 

The following condition is always evaluated as true ; 以下条件始终被评估为true even if it's false || 'a' 即使是false || 'a' false || 'a' , 'a' ~> true : false || 'a''a' a'〜> true

if(addOrRemove == 'A' || 'a'){ ...

it was meant to be: 这是命中注定的:

if(addOrRemove == 'A' || addOrRemove == 'a'){ ...

However when addOrRemove is declared as a char , then: 但是,当addOrRemove声明为char ,则:

cin >> addOrRemove;

might just read a new-line character or some white space. 可能只是读取换行符或空白。 It would be probably more reasonable to declare addOrRemove as std::string and change your condition to: addOrRemove声明为std::string并将条件更改为:

if(addOrRemove == "A" || addOrRemove == "a"){ ...

And after you read choice and it's 0 , you should break your loop so that it won't try to access element at index 0 - 1 : 在您读取了choice并且它是0 ,您应该break循环,以便它不会尝试访问索引0 - 1元素:

cin >> choice;
if (choice == 0) break;   // <-- THIS

First of all instead of 首先代替

if(addOrRemove == 'A' || 'a'){

you should write 你应该写

if(addOrRemove == 'A' || addOrRemove == 'a'){

And secondly you should define variable count because it seems that the compiler thinks that count - is name of standard algorithm std::count. 其次,您应该定义变量count,因为似乎编译器认为count是标准算法std :: count的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM