在特定时间终止循环的问题

时间:2015-07-19 03:05:00

标签: c++ for-loop command-line

我的循环出现问题。该程序应该类似于打印出来的 乐透彩票。用户输入他/她想要多少套乐透号码。每行都按字母顺序标记,但如果有人想要更多十行(字母J)的乐透号码,那么程序应该再次从A开始。我的问题是,如果有人输入10(或任何10个间隔),“mega”会像这样打印出来:

enter image description here

如果还有另一行乐透号码,则只能再次打印“Mega”。 在“for main()”中,“for()”是我试图解决这个问题。

#include <iostream> //I/O
#include <iomanip> //setw
#include <ctime>  //seeding srand
#include <string> //size
#define RAND(a,b) (a+rand()% (b-a+1))
#define die(errmsg) {cerr << errmsg << endl; exit(1);}
using namespace std;

/*
  Author: Zachary Stow
  Date: July/20/15
  Homework #5
  Objective: To design a program that imitates the print out
             of a lottery ticket.
*/

//********************************fillup()********************************
void fillup(int lotto[], int n, int from, int to)
{ 
    void bubble_sort(int x[], int n);

    for(int i = 0; i < n; i++)
    {
        lotto[i] = RAND(from,to);
    }
    bubble_sort(lotto,5);
}

//*****************************bubble_sort()******************************
void bubble_sort(int x[], int n)   
{
    for(int i = 0; i < n-1; i++)
    {
        int temp;

        for(int j=i+1; j<n ; j++)
        {
            if(x[i] > x[j])
            {
                temp = x[i];
                x[i] = x[j];
                x[j] = temp;
            }
        }
    }        
}


//********************************print()*********************************
void print(int x[], int n)
{
    for(int i = 0; i < n; i++)
    {       
        cout << setfill('0') << setw(2) << x[i] <<" ";
    }
    cout <<" ";
    cout << setfill('0') << setw(2) << RAND(1,46);  
    cout << endl;
}

//********************************isNumber********************************
bool isNumber(string str)
{
    for(int i = 0; i < str.size(); i++)
    {
        if(!isdigit(str[i]))return(false);
    }

    return(true);
}

//**********************************check*********************************
void check(int argc, char **argv)   
{
    bool isNumber(string);

    if(argc != 2)die("usage: megaMillion number_tickets");

    string num_tickets = argv[1];
    if(!isNumber(num_tickets))die("Not a digit.");  //removed num_tickets for now

    int num;
    num = atoi(num_tickets.c_str());    
    if(num <= 0)die("Zero or negative number.");    //doesnt work
}

//*********************************printmega()****************************
void printmega(int letter)
{
    if(letter == 65)
        {
            cout << endl;
            cout <<"                  Mega" << endl; //10 you get a mega
        }  
}

//*********************************main()*********************************
int main(int argc, char **argv)
{
    void fillup(int x[], int n, int from, int to);
    void print(int x[], int n);
    void check(int argc, char **argv); 
    void printmega(int letter);

    check(argc, argv);
    srand(time(NULL));                   

    cout <<"                  Mega" << endl;   

    int letter = 65;

    for(int i = 0; i < atoi(argv[1]); i++)
    {
        if(i == atoi(argc[1]))cout << "Hi"; //my attempt to stop the loop from printing
                                            //only mega after J
        cout <<(char)letter++;              //when theres no more lines
        cout <<"  ";

        if(letter == 75)letter = 65;

        int lotto[5];

        fillup(lotto,5,1,56);                     

        print(lotto,5);

        printmega(letter); 
    }

    return(0);                  
}

1 个答案:

答案 0 :(得分:0)

对于这个问题,我会选择使用两个嵌套循环,这是一个最小的例子:

#include <iostream>

int main(int argc, char** argv)
{
  double aValues[] = {1, 2, 3, 4, 5, 6, 7, 8, 6, 2};
  int nTotalLines = sizeof(aValues)/sizeof(double);
  int nLinesPerBlock = 5;

  int nLineNumber = 0;
  for (int i = 0; i <= nTotalLines/nLinesPerBlock
      && nLineNumber < nTotalLines; ++i)
  {
    std::cout << "Start of block..." << std::endl;
    for (int j = 0; j < nLinesPerBlock && nLineNumber < nTotalLines; ++j)
    {
      std::cout << "  Number: " << aValues[nLineNumber++] << std::endl;
    }
  }

  return 0;
}

您需要调整此代码以适合您的问题。没有必要使用嵌套循环,但对我来说似乎更合乎逻辑(你为每个块循环一个外循环,为每一行循环一个内循环)。

另一个提示:使用变量,这样您就不需要继续输入atoi(argv[1])之类的内容,而是创建变量int name = atoi(argv[1]);并在需要的地方使用name