4 Replies - 1069 Views - Last Post: 13 December 2014 - 02:46 AM Rate Topic: -----

#1 music_in_me227   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-December 14

C++ homework, please help..

Posted 11 December 2014 - 10:27 AM

Hey there! I'll be really thankful if you help me with this task...I'm translating it from my language so there might be mistakes but I hope you're going to understand what I'm talking about:

Create a program with function main() and menu with functions for:
A) Inserting several numbers from the keyboard into one-dimensional array (the numbers should be less than 20);
B)/> Overwriting of the 1st array into second array in which the elements are put in reverse order.
C) Overwriting of the second array into third array in which the elements are put in descending order.
D)Displaying information about the array. (I guess this is A,B and C)



I'm really not good in C++ so I don't need anything complicated. This is what I have done so far:
#include <iostream>
using namespace std;
const int MAX_SIZE = 20;
int inserting (int elements []);
int reverseorder (int elements []);
int descendingorder (int elements[]);
int display (int elements[]);

int main(){
	int choice;
	cout << "1. Insert several numbers" << endl;
	cout << "2. The numbers in reverse order" << endl;
	cout << "3.The numbers in descending order"<< endl;
	cout << "4. Display array with information"<<endl;
	cout << "\n Type your choice here:" ;
	
	cin >> choice;
	if (choice < 5 && izbor > 1) continue;	// here I get a message that continue statement is not withing a loop
	switch (choice)
	{
		
	case 1: int inserting (int elements []);
	case 2: int reverseorder (int elements []);
	case 3: int descendingorder (int elements[]);
	case 4:	int display (int elements[]);

	} // I am not really sure if the whole switch statement is correct...I get a message "Error:expected primary-expression before "int"".

After I do this I have to start writing the functions but I find difficulty to do it....Any help will be appreciated...


Thanks in advance,
:)/>

This post has been edited by modi123_1: 11 December 2014 - 10:28 AM
Reason for edit:: Please use the 'code' button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: C++ homework, please help..

#2 b0nes   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 45
  • Joined: 10-July 14

Re: C++ homework, please help..

Posted 11 December 2014 - 12:54 PM

Hello!

When I tried to compile your code using ideone.com I got the following compiler errors

prog.cpp: In function ‘int main()’:
prog.cpp:19:20: error: ‘izbor’ was not declared in this scope
if (choice < 5 && izbor > 1) continue;
^
prog.cpp:19:31: error: continue statement not within a loop
if (choice < 5 && izbor > 1) continue;
^
prog.cpp:28:2: error: expected ‘}’ at end of input
}
^

Perhaps it is from the translation, but if izbor is a variable, it needs to be declared within the scope before you use it,

adding for example, int izbor = 0; should help.

I believe we can get rid of the continue; as well and just set it up like this

if (choice < 5 && izbor > 1)
{
switch ( stuff )
{

case 1:
case 2:
}
}

Also, don't forget to add breaks to your case statements! Check out this website for some tutorials on switch/case, and other things. They really helped me out when I was getting started

http://www.learncpp.com/

http://www.learncpp....tch-statements/ For switch/case stuff.

Also, don't forget to add another } to the end of your main, it looks like you're missing one

This post has been edited by b0nes: 11 December 2014 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: C++ homework, please help..

Posted 11 December 2014 - 03:23 PM

The error you're receiving is because you're re-declaring your functions in the switch-case statement. Remove "int" from the beginning of the functions so you don't redeclare them. Also, you need to pass in an array, not an array declaration.

#include <iostream>
using namespace std;
const int MAX_SIZE = 20;
int inserting (int elements []);
int reverseorder (int elements []);
int descendingorder (int elements[]);
int display (int elements[]);

int main(){
	int choice;
	int array_1[MAX_SIZE];                               // Declare first array
	int array_2[MAX_SIZE];                               // Declare second array
	int array_3[MAX_SIZE];                               // Declare third array
	cout << "1. Insert several numbers" << endl;
	cout << "2. The numbers in reverse order" << endl;
	cout << "3.The numbers in descending order"<< endl;
	cout << "4. Display array with information"<<endl;
	cout << "\n Type your choice here:" ;
	
	cin >> choice;
	if (choice < 5 && izbor > 1) continue;	// here I get a message that continue statement is not withing a loop
	switch (choice)
	{
		
	case 1: 
	    inserting (array_1);
	    break;
	case 2: 
	    reverseorder (array_2);
	    break;
	case 3: 
	    descendingorder (array_3);
	    break;
	case 4:	
	    display (array_1, array_2, array_3);
	    break;
	default:
	    break;

	} // I am not really sure if the whole switch statement is correct...I get a message "Error:expected primary-exp<b></b>ression before "int"".


Was This Post Helpful? 0
  • +
  • -

#4 music_in_me227   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-December 14

Re: C++ homework, please help..

Posted 13 December 2014 - 01:40 AM

Hey,I tried the code which IngeniousHax wrote but I get this message when I try to run the program:

Posted Image
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: C++ homework, please help..

Posted 13 December 2014 - 02:46 AM

> int inserting (int elements []);
Well, having written this, you've made a promise to at some point write this.

int inserting (int elements []) {
  // do something which resembles inserting into the array
  // what you insert, how many you insert, and where you
  // get them from is up to you.
}



> 21 if (choice < 5 && izbor > 1) continue; // here I get a message that continue statement is not withing a loop
For now, just delete this line.
Error checking and retry can come later.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1