Array project

I have an array assignment for beg. programming, but he did not give any instructions, or explanation of purpose of the program. Just a list of C++ logic. I get confused by this stuff as it is, but it really helps me understand better what I need to do when I know what the program needs to accomplish. I asked my professor but he just keeps responding to "input the logic". Not very helpful. I have included the instructions below. I am not asking anyone to write this code for me, just hoping someone can help me understand what the purpose of the program will be. And is he wanting us to input this logic exactly as written, in that order? Meaning, we will not be using any functions in this code? I get the beginning is declaring variables and arrays and data types, and I think the get name address etc just means a cout/cin section to ask the user to input those variables. But when it gets in to the if else section, I am confused by what he gives because not every if has an else, and after the endwhile there is a randomn if with no "for" before. By "while", did he mean "for"? Is this just using one dimensional arrays? Any direction anyone could provide would be most appreciated!
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
Implement the following logic in C++, Use appropriate data types. Data types are represented as either numeric (num) or string. Submit your zipped file. 

start
string name
string address
num item
num quantity
num price
num SIZE = 6
num VALID_ITEM [SIZE] = 106, 108, 307, 405, 457, 688
num VALID_ITEM_PRICE [SIZE] = 0.59, 0.99, 4.50, 15.99, 17.50, 39.00
num sub
string foundIt = “N”
string MSG_YES = “Item available”
string MSG_NO = “Item not found”
get name, address, item, quantity
sub = 1
while sub <= SIZE
if item = VALID_ITEM [sub] then
foundIt = “Y”
price = VALID_ITEM_PRICE [sub]
endif
sub = sub + 1
endwhile
if foundIt = “Y” then
print MSG_YES
print quantity, “ at “ , price, “ each”
print “Total “, quantity * price
else
print MSG_NO
endif
stop
Last edited on
Okay.

Basically, what you see is program written in pseudocode. You have to read it using instructions and try to understand it. Then, rewrite it in C++.
First, you have your variables declarations(strings and numbers - probably integers).
After that, you have a while loop(if you have not used it, google it or while loop tutorials).
Inside body of while loop you have if conditional statement, and one afterwards.

Basically try to rewrite this code. Remember - it is pseudocode. It is thought to be kind of universal for all programmers. If you see while - you know while loop starts there. If you see endwhile - you know that while loop has to end there. Don't complicate things, KISS, and you will be fine :)

And after you code and find a problem you can't solve - just ask.

Cheers!
Thanks Matthew! Ok I've written our the psuedoecode in C++, but am getting some errors. All of my cout/cin commands are saying they have no storage class or type specifier, but I included iostream so I'm not sure why that's happening. Also basically my entire while and if section is getting an error message "expected a decleration". Any ideas on how to fix the errors? Thanks in advance!

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

using namespace std;

int main ()

string name, address;
int item, quantity;
double price, total = quantity * price;
int SIZE = 6;
int VALID_ITEM [] = {106, 108, 307, 405, 457, 688};
int VALID_ITEM_PRICE [] = {0.59, 0.99, 4.50, 15.99, 17.50, 39.00};
int sub;
string foundIt = "N";
string MSG_YES = "Item available";
string MSG_NO = "Item not found";

	cout << "Please enter your name: ";
	cin >> name;

	cout << "Please enter your address: ";
	cin >> address;

	cout << "Please enter the item: ";
	cin >> item;

	cout << "Please enter the item quantity: ";
	cin >> quantity;

sub = 0
while (sub <= SIZE)
	if (item = VALID_ITEM [sub])
		foundIt = “Y”
		price = VALID_ITEM_PRICE [sub]; 
		sub = sub + 1
	
if (foundIt = “Y”)
	cout << MSG_YES
	     << quantity, " at $" , price, " each", /n
	     << "Total: ", total;
else
	cout << MSG_NO;

return 0;
Blocks of code need curly braces around then. So stuff like
1
2
3
4
5
6
7
8
9
10
int main() {
  ...
  if (something) {
    ...
  }
  else { ... }
  while (something) {
    ..  
  }
}


Also, make sure your lines end with a semicolon.
Last edited on
jstew07 - have you used C++ before? You forgot quite a few things.

First off - int main() has curly brackets {} in which you hold the code :) I suppose that rewriting the code made you forget about it :) That's the error #1.

price and total should be ints as well - integer multiplied by integer is integer. No need to make it of double type. Also, you had only 2 types - num and string. Read notes from professor carefully.

And later in code, same goes for while loop and conditional statements - remember about curly brackets {}.

Cheers!
Last edited on
Topic archived. No new replies allowed.