Hi, I started trying to learn c++ yesterday, I understand the logic of what I need to do and in what order (I think!) but have problems with the codes themselves as you would expect from a beginner! So first of all I did a few practice programs after watching a ton of youtube videos and reading bits and peices for beginners online.
The first program was to create a "vending machine" that gave you 5 choices of drinks and gave you an error message if you did not select 1-5. I decided to take the validation a bit further and get it to throw errors if you enter non numerical values and as asked dont enter a numerical value of 1-5. After the error happens I am wondering how I can get the code to go back so the user can input another choice.
Another little niggling thing thats irritating me for the sake of neatness is lines 25-28 , how can I in one statement say if its less than 1 or greater than 5 then.... tried this for a while and just could not find any operaters that worked for me :(
I just add the system pause to stop it from exiting when I run the program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
unsigned int DrinkSelect; //unsigned int cant go below 0
cout << "::Drinks Vendor:: \n\n";
cout << "Please select which number drink you would like \n";
cout << "1 - Coke \n";
cout << "2 - Sprite \n";
cout << "3 - Water \n";
cout << "4 - Orange \n";
cout << "5 - Lemon \n \n";
cout << "Drink select: ";
if (!(cin >> DrinkSelect)) //if input for drinkselect is not an integer
{
cout << "This is not a number \nClosing Program";
}
else if (DrinkSelect <= 1) {
cout << "\n \nInvalid selection";
} else if (DrinkSelect >=5) {
cout << "\n \nInvalid selection";
} else if (DrinkSelect == 1) {
cout << "\n \nYou selected Coke";
} else if (DrinkSelect == 2) {
cout << "\n \nYou selected Sprite";
} else if (DrinkSelect == 3) {
cout << "\n \nYou selected Water";
} else if (DrinkSelect == 4) {
cout << "\n \nYou selected Orange";
} else if (DrinkSelect == 5) {
cout << "\n \nYou selected Lemon";
}
system("PAUSE");
}
| |
The next project I decided to try was to create a program that told you how much physical ram you had on your machine. I know im only taking babysteps but I want to take it further once I get there and then then set your paging file to manual with an input that is based off your ram. I am guessing the general concencus would be for me not to mess around with API's until I have half a clue about the most basic c++ programming but I couldnt help but give it a go!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
BOOL WINAPI GetPhysicallyInstalledSystemMemory(
_Out_ PULONGLONG TotalMemoryInKilobytes
);
system("pause");
}
| |
I found that code on msdn and to tell you the truth although I know what it does I have no idea how it does it , I checked in Windows.h to see If I could make more sense of it but not alot, so if someone would care to explain whats going on here I would greatley appreciate it.
So I am thinking I want to create a variable called like "Ramsize" that would be a "long int" and placed above the BOOL section. I have no idea how I then get that BOOL to store the result it receives into "ramsize" so after I can just have:
cout << "Your Ramsize is " << Ramsize ;
All of this is on Windows , please feel free to shred my horrible coding and tell me how I could improve it , thanks for your time!
Ben.