Skip to content

Instantly share code, notes, and snippets.

@brhoades
Created September 14, 2016 17:47
Show Gist options
  • Save brhoades/0c0bcf58da655b4c1009d41b1a802faf to your computer and use it in GitHub Desktop.
Save brhoades/0c0bcf58da655b4c1009d41b1a802faf to your computer and use it in GitHub Desktop.
Soda Dispenser - No functions and I didn't make an array ;D!
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cstdlib>
#define NUMBER_OF_OPTIONS 3
#define NUMBER_OF_ARGS NUMBER_OF_OPTIONS*2
using namespace std;
int main(int argc, char** argv)
{
int counter=1;
if(argc == 1)
{
// If you don't want to use arrays, provide your own data.
char* newargv[] = {"3", "Dr. Pepper", "1", "Pepsi", "2", "SQRT Beer"};
argc = NUMBER_OF_ARGS;
argv = newargv;
}
else if(argc % 2)
{
cerr << "Must have an even number of arguments." << endl;
return 0;
}
cout << "Choose a drink to vend:" << endl;
int choice = 255;
for(int i=0; i<argc; i+=2)
{
if(atoi(argv[i]) == 0)
continue;
cout << " " << counter << ") " << argv[i+1] << " (" << argv[i] << " left)" << endl;
counter++;
}
if(counter == 1) // we never printed a menu item
{
cout << " OUT OF ORDER: EMPTY" << endl;
return 0;
}
cout << " " << counter << ") Exit" << endl << endl;
cout << "Choice: ";
cin >> choice;
if(choice == counter)
{
cout << "Thank you! Please come again!" << endl;
return 0;
}
if(choice > counter || choice == 0)
return main(argc, argv);
choice -= 1;
// Finally, actually do something since it's a valid choice
for(int i=0; i<=choice; i++)
{
if(atoi(argv[i*2]) == 0)
{
choice++;
continue;
}
}
char new_value[8];
sprintf(new_value, "%u", atoi(argv[choice*2]) - 1);
argv[choice*2] = new_value;
cout << "A cool " << argv[choice*2+1] << " falls down into the collection chute." << endl;
return main(argc, argv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment