2013-11-26 6 views
2

Мне не нужна небольшая помощь в выполнении домашней работы. Это проект в соответствии с тем, что мне нужно объединить 2 программы, и пользователь может сделать выбор, какую программу он хочет запустить; Я пытаюсь использовать инструкцию IF ELSE для этого. При выполнении этих программ по отдельности они работают нормально. Xcode дает мне проблему при попытке запустить первую программу rand function one. Можете ли вы, ребята, помочь мне? БлагодаряОшибка в Xcode при объединении двух функций

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

int main() 
{ 
    int x; 

    cout << "Hello Welcome to my Game Menu. Please make selection from following criteria" <<endl; 
    cout << "Please press 1 if you would like to play The Guessing Game"<<endl; 
    cout << "Please press 2 if you would like to play The Math Game"<<endl; 
    cin >>x; 

    if (x == 1) 
    { 
     cout << "Welcome to Guessing Game" <<endl; 
     //declare the variables 
     int num; //variable to store the random 
     //number 
     int guess; //variable to store the number 
     //guessed by the user 
     bool isGuessed; //boolean variable to control 
     //the loop 

     srand(time(NULL)); //Line 1 

     num = rand() % 100; //Line 2 

     isGuessed = false; //Line 3 

     while (!isGuessed) //Line 4 
     { //Line 5 
      cout << "Enter an integer greater" 
      << " than or equal to 0 and " 
      << "less than 100: "; //Line 6 

      cin >> guess; //Line 7 
      cout << endl; //Line 8 

      if (guess == num) //Line 9 
      { //Line 10 
       cout << "You guessed the correct " 
       << "number." << endl; //Line 11 
       isGuessed = true; //Line 12 
      } //Line 13 
      else if (guess < num) //Line 14 
       cout << "Your guess is lower than the " 
       << "number.\n Guess again!" 
       << endl; //Line 15 
      else //Line 16 
       cout << "Your guess is higher than " 
       << "the number.\n Guess again!" 
       << endl; //Line 17 
     } //end while //Line 18 
    } 

    else if (x == 2) { 
     cout <<"Welcome to Math Game " <<endl; 

     double a,b,x,y,z,sum; 
     string name; 

     cout << "Please enter your name... "; 
     cin>> name; 
     cout<<endl; 
     cout << " WELCOME TO MATH MIND TRICKS " <<name <<endl; 
     cout<< "I will read your mind." <<endl; 
     cout<< "We will choose five , five digit numbers together" <<endl; 
     cout<< "but i will be able to give you the sum, just after the first one" <<endl; 
     cout<<endl; 
     cout<< "What is your first five digit number?"<<endl; 
     cin>> x; 
     cout<<endl; 
     cout<< " The sum is going to be " << 199998+x <<endl; 
     cout<< "Please enter a second number" <<endl; 
     cin>> y; 
     cout<<endl; 
     a= 99999-y; 
     cout<< " OK, im going to choose " <<a <<endl; 
     cout<< " Please enter your last five digit number" <<endl; 
     cin>> z; 
     b= 99999-z; 
     cout<< " OK, im going to choose " <<b <<endl; 
     cout<<endl; 
     sum = a+b+x+y+z; 
     cout<< " The sum of our five numbers is " <<sum <<endl; 
     cout<<endl; 
     cout <<"Impressed? :)" <<endl; 
    } 
    return 0; 
} 
+2

Какую ошибку вы получаете? –

+0

Он работает нормально, я его протестировал. –

+0

Эта ошибка http://i41.tinypic.com/10xdmc5.png – user3035168

ответ

1

Время() возвращает NSUInteger, и на 64-битной OS X платформы

  • NSUInteger определяется как беззнаковое долго, и
  • беззнаковое долго это 64-разрядное unsigned integer.
  • int - это 32-разрядное целое число.

Таким образом, int является «меньшим» типом данных, чем NSUInteger, поэтому предупреждение компилятора.

Смотрите также NSUInteger в "Типы данных Foundation Reference":

При создании 32-разрядных приложений, NSUInteger является 32-разрядное целое число без знака. 64-разрядное приложение рассматривает NSUInteger как 64-разрядное целое число без знака.

Чтобы исправить это предупреждение компилятора, вы можете либо объявить локальную переменную счетчика, как

NSUInteger count; 

или (если вы уверены, что ваш массив никогда не будет содержать более 2^31-1 элементы!), Добавить явное литье:

srand((int)time(NULL)); 
+0

Спасибо, что вы мужчина! :) – user3035168

Смежные вопросы