2016-11-21 163 views
-7

ODDAVG - 编写一个程序来接受来自键盘的整数并找出10个奇数的平均值。如果输入的是偶数,则忽略计算中的数字,然后打印出“仅限奇数”的消息。我想要帮助此程序,我尝试了一个多小时?C++ odd平均值

#include "stdafx.h" 
#include <iostream> 
#include <iomanip> 
#include <string> 


using namespace std; 

void Pause()  
{ // function to freeze the screen, waiting for a `keypress` 

    string junk; 
    cout << "\n\n Press enter to continue..."; 
     cin.ignore(); 
    getline (cin,junk); 
    // void functions do not return a value 
} 



int main() 

{ 
    int even=0; 
    int odd=0; 
    int number=0; 
    int eventotal = 0; 
    int oddtotal = 0; 
    int counter = 0; 
    bool done; 
    int i; 

    // get `inpot` 
     cout << "Please enter 10 odd integers: " << endl << endl; 
     cin >> number; 
     cout << endl;  


    // display results 

     if ((number%2 == 0) && (number > 0)) 
     { 
     cout << "odd numbers only please." << endl; 
     } 

     else if ((number > 0) && (i++)) 
     { 
      odd++; 
      oddtotal = oddtotal +number; 

     } 

    while (number!=0)(!done); 
      counter ==10; 
      counter++; 


    int oddavg ;// to store the average of odd numbers 
     if(oddtotal!=0) 
      { 

       oddavg = oddtotal/ odd; 
      } 
      cout << " The average is: " << oddavg << endl; 


// freeze screen 
    Pause(); 
    return (0); 
} 
+2

你已经明确地从某处复制过它。它甚至没有编译代码中的所有“'”。你试过什么了 ? – Arunmu

+0

什么是'while(number!= 0)(!done);'应该这样做? –

+0

把代码编译.. –

回答

2

不要只是复制代码,试着理解它。即使你只是想通过一个班级,“捷径”也不会帮助你。你将在决赛中独处,并在长期的职业发展中落后。是的,即使你的专业不是计算机科学(从这里的经验来讲)。

一些有用的资源:

C++ functions

C++ loops

C++ recursion

这最后一个,递归,基本上都采用本身就意味着一个函数,这是关键到您的问题。

验证数字是否奇数的函数需要不断调用自身,以便模拟所需的“暂停”。有一个实际的暂停是不是切合实际,为您的目的。

话虽这么说,分析工作代码为你的问题如下,它正是你想要的:

#include <iostream> 
#include <string> 

using namespace std; 

const int NUMBER_COUNT = 10; // pre-defined numbers to input 
int checkOdd(int input, int count); 

int main() 
{ 

int oddNums[10]; 
double avg, sum = 0; 

// Instructions 
cout << "Enter 10 numbers, Odd Numbers Only: "<<endl; 

// Input numbers 
for (int count = 0; count < NUMBER_COUNT; count ++) 
{ 
    cout << "Odd Number #"<<count+1<<": "; 
    int input; 
    cin >> input; 
    oddNums[count] = checkOdd(input, count+1); 
} 

for (int count = 0; count < NUMBER_COUNT; count++) 
{ 
    sum += oddNums[count]; 
} 

// Average 
avg = sum/NUMBER_COUNT; 

// output average 
cout << "Average of Odd numbers: "<< avg <<endl; 

return 0; 
} 


int checkOdd(int input, int count) { 

    if(input % 2== 0) { 
     int newAnswer; 
     cout << input<<" is not an odd number, try again!!"<<endl<<"Odd Number #"<<count<<": "; 
     cin >> newAnswer; 
     return checkOdd(newAnswer,count); 
    } 
    else{ 
     return input; 
    } 
} 

干杯,给予好评&如果它有助于为答案选择。

+0

谢谢你的建议。我会买它。 – Tareq

0

老实说,一个小时对编程任务来说没有任何意义。有时你会完成并花一个小时找到一个简单的错误。

至于回答你的问题,我诚实地相信你所拥有的代码比它的难度要大得多。

这是我简单的一个简单的问题。

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

int main(){ 
    double sum_odd = 0; 
    int input = 0; 
    int count = 0; 
    double ans = 0; 

    //loops until 10 odd numbers have been entered 
    while (count < 10){ 
     cout << "Input 10 odd numbers: "; 
     cin >> input; //assuming user is putting in integers 
     while(input % 2 == 0){ 
      cout << "You have input an even number, please enter an odd number"; 
      cin >> input; 
     } 
     sum_odd += input; 
      count++; 
    } 

    //find the average of the 10 numbers 
    ans = sum_odd/10.0; 
    cout << ans; 
    return 0; 
} 
+0

谢谢你的帮助 – Tareq