需要帮助创建我的C ++程序

时间:2015-03-18 15:36:46

标签: c++

我在创建C ++程序时遇到问题。我希望在输入'N'时不播放声音。如果按'Y',声音就没有问题了。

我正在创建自己的程序,我想让它在输入'Y'以外的任何内容时不播放声音。我正在Visual Studio 2010中创建一个控制台应用程序。

现在,即使输入'n',程序也会播放声音。 这是代码:

// Dial Up Console.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#pragma comment(lib, "winmm.lib")
#include <windows.h>
#include <mmsystem.h>
using namespace std; 

char dialy;

char bonzi;


int playsound(); 


int main()
  {
     cout << "Do you like the handshake sound of dial up?"; 

     cin >> dialy;

     if (dialy=='y')
       {
         cout <<"Here you go! Enjoy! \n"; 
         PlaySound(TEXT("C:\\Users\\Sam\\Dropbox\\Visual Studio 2010\\Projects\\Dial Up Console\\dialup.wav"), NULL, SND_FILENAME | SND_ASYNC); // I don't want the sound to play if N is entered. 
       }

     if (dialy=='n')             
       {
          cout <<   "Have you heard of BonziBUDDY? \n";
          cin >> bonzi;
       }
     else    
       {
          cout << "Sorry dude! I don't understand! boogey ball! baby!";
       }
     if (bonzi=='y')
       {
          cout << "Good. You were born a long to know that dial up Internet existed. Here is a nice little sound clip for you to enjoy!";
       }
     else if (bonzi=='n')
       {
         cout << "Congratulations! You do not slow Internet and you're not old. Goodbye baby!";
       }
     else 
       {
          cout << "Sorry dude! I don't understand! boogey ball! baby!";  
       }
     system("pause");
     return 0; 
  }

2 个答案:

答案 0 :(得分:1)

输入是否是&#39; y&#39;或不是,Playsound将因为括号而执行。

应该是

if (dialy=='y')
{
         cout <<"Here you go! Enjoy!";
      PlaySound(TEXT("C:\\Users\\Sam\\Dropbox\\Visual Studio 2010\\Projects\\Dial Up Console\\dialup.wav"), NULL, SND_FILENAME | SND_ASYNC); // I don't want the sound to play if N is entered. 
     }

*我只检查了第一个if条件。

答案 1 :(得分:0)

根据我的理解,我已将您的代码更改为

int main()
  {
     cout << "Do you like the handshake sound of dial up?"; 
     cin >> dialy;

     if ( dialy == 'y' || dialy == 'Y' )  // checks for capital and small letter
       {                                 // This is where the main problem was. Only the cout statement was in the if block
         cout <<"Here you go! Enjoy! \n";       
         PlaySound(TEXT("C:\\Users\\Sam\\Dropbox\\Visual Studio 2010\\Projects\\Dial Up Console\\dialup.wav"), NULL, SND_FILENAME | SND_ASYNC); // I don't want the sound to play if N is entered. 
       }

     else if ( dialy == 'n' || dialy == 'N' )   // changed to else if
       {
         cout << "Have you heard of BonziBUDDY? \n";
         cin >> bonzi;

         // I moved some part of your code into this if block

         if ( bonzi == 'y' || bonzi == 'Y' ) 
           {
              cout << "Good. You were born a long to know that dial up Internet existed. Here is a nice little sound clip for you to enjoy!";
           }

         else if ( bonzi == 'n' || bonzi == 'N' )   
           {
             cout << "Congratulations! You do not slow Internet and you're not old. Goodbye baby!";
           }

         else 
           {
             cout << "Sorry dude! I don't understand! boogey ball! baby!";
           }

       }

     else
       {
         cout << "Sorry dude! I don't understand! boogey ball! baby!";
       }

    system("pause");
    return 0; 
  }

问题在于

 if (dialy=='y')

     cout <<"Here you go! Enjoy! \n"; 
 {
  PlaySound(TEXT("C:\\Users\\Sam\\Dropbox\\Visual Studio 2010\\Projects\\Dial Up Console\\dialup.wav"), NULL, SND_FILENAME | SND_ASYNC); // I don't want the sound to play if N is entered. 
 }

只有cout位于if块内,并且PlaySound()将被调用,无论您输入什么(请参阅注释)。

这应该是

 if ( dialy == 'y' || dialy == 'Y' )
   {
     cout <<"Here you go! Enjoy! \n"; 
     PlaySound(TEXT("C:\\Users\\Sam\\Dropbox\\Visual Studio 2010\\Projects\\Dial Up Console\\dialup.wav"), NULL, SND_FILENAME | SND_ASYNC); // I don't want the sound to play if N is entered. 
   }

我还修复了此代码中的一些逻辑错误。

请注意,我在if语句中添加了|| dialy == 'Y',以便也接受大写字母(我也添加了对其他If语句的类似编辑)。