0
STRUCT問題1.cpp(C++)的簡單問題做與結構和功能參數
// Struct Problem 1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "structs.h"
double calculateEarnings(Advertising aDay);
void displayEarnings(int questionInstance, Advertising aDay);
void mainQuestions();
int _tmain(int argc, _TCHAR* argv[])
{
mainQuestions();
return 0;
}
double calculateEarnings(Advertising aDay)
{
std::cout << aDay.usersClicked; //debug
return aDay.totalAdsShown * aDay.usersClicked * aDay.averagePerAd;
}
void takeInData(int questionInstance, Advertising aDay)
{
if (questionInstance == 0)
{
std::cin >> aDay.totalAdsShown;
}
else
if (questionInstance == 1)
{
std::cin >> aDay.usersClicked;
}
else
if (questionInstance == 2)
{
std::cin >> aDay.averagePerAd;
}
std::cin;
}
void mainQuestions()
{
static Advertising aToday;
aToday.totalAdsShown = 0;
aToday.usersClicked = 0.00;
aToday.averagePerAd = 00.00;
std::cout << "Welcome! Please input the advertising data for today." << "\n";
std::cout << "How many ads were shown today?" << "\n";
takeInData(0, aToday);
std::cout << "What percentage of users clicked our ads? (decimal form)" << "\n";
takeInData(1, aToday);
std::cout << "What were the average earnings per ad? (ex: 5.15)" << "\n";
takeInData(2, aToday);
structs.h
#ifndef STRUCTS_H
#define STRUCTS_H
typedef double percentage;
typedef double USD;
struct Advertising
{
int totalAdsShown;
percentage usersClicked;
USD averagePerAd;
};
#endif
基本上,當由 'CIN' 稱爲不保存在數據。我添加了一行來打印aDay.usersClicked值,它打印爲0.我正在學習C++,所以這個問題是非常基本的。我欣賞所有提示!
謝謝