Returning back to inputs and finding ram

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.
any suggestions?
1 more up :)
Hey aggsyb.

before I start I just want to say that I'm a beginner too so take what I say with a grain of salt.

a couple of problems I found with your Vending Machine code

1
2
3
4
else if (DrinkSelect <= 1) {
cout << "\n \nInvalid selection";
} else if (DrinkSelect >=5) {
cout << "\n \nInvalid selection 


here you made an error by saying <= (less then or EQUAL to) which gives back the invalid selection when you choose 1 for Coke and the same back when you choose 5 for Lemon what you want to enter is simply < and >.

Here's how I'd write the program personally:

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
#include <iostream>

using namespace std;


int main()
{
		
			char selection;

			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: ";
			

			cin >> selection;

			switch(selection) {
				case '1': cout << "You selected Coke" << endl; break;
				case '2': cout << "You selected Sprite" << endl; break;
				case '3': cout << "You selected Water" << endl; break;
				case '4': cout << "You selected Orange" << endl; break;
				case '5': cout << "You selected Lemon" << endl; break;
				default: cout << "invalid selection" << endl; break;
			}
			
		system("PAUSE");
	
}
 


As for your second program, its a bit out of my range at the minute but I like your way of learning!
how can I in one statement say if its less than 1 or greater than 5 then....


if( drinkSelect < 1 || drinkSelect > 5 )


After the error happens I am wondering how I can get the code to go back so the user can input another choice.


Wrap the code in a loop, e.g.:

1
2
3
4
5
6
7
8
bool flag = true;

while(flag)
{
     // ...
     if( something ) flag = false;
}


Set the flag to false if you want to stop looping. (or just break out of the loop with break;).
Last edited on
Neil,

Thanks for taking the time to reply, I really appreciate it!, the code looks good I will give it a try when I get home!
Topic archived. No new replies allowed.