0
我的循環有問題。該程序應該類似於打印出一張 樂透彩票券。用戶輸入他/她想要的多少組樂透號碼。每行都按字母順序標記,但如果有人想要樂透號碼超過十行(字母J),程序應該再次從A開始。我的問題是,如果有人進入10「特大」(或十個任何間隔)被印像這樣:在特定時間終止循環的問題
「兆豐」只應再次打印,如果有樂透號碼的另一條線。 在「for()」裏面的「int main()」是我試圖解決這個問題的。
#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);
}
發佈時嘗試創建一個仍然可以演示您的問題的最小示例。在這個問題中,我們並不需要知道你是如何實現你的冒泡排序的。 – ilent2
這似乎與在由逗號分隔的列表中輸出元素的問題類似。天真的方法要麼打印逗號,空格和元素,要麼打印元素,逗號和空格。第一個元素不好,因爲第一個元素之前有一個逗號。第二,因爲在最後一個元素之後有一個逗號。在這種情況下,修正是打印一個逗號和一個空格_IF_這不是第一個元素,_THEN_只打印一個元素。這也是很自然的,也是我們在紙上列出清單的方式。我想,同樣的方法可以解決您的問題。 ;) – enhzflep
請注意,在「嘗試停止循環」中有一個拼寫錯誤argc是一個整數,您可能意味着argv [1]或者只是寫'if((i%10)== 0)...'十是你的程序中的固定限制。 –