d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Really Noob C++ Code
Add Reply New Topic New Poll
Member
Posts: 33,927
Joined: Oct 9 2008
Gold: 2,528.52
Feb 10 2014 04:56pm
This code is supposed to do a sum, mean, max, and average function. That part isn't a problem, but I can't get it to compile.

The error message is listed at the bottom. Props to anyone who can fix this quick

Code

#include <iostream>
#include <cstdlib>

int main(int argc, char* argv[]) {
int index=2;
//proper return value
int mainChoice=0;

//Make sure there is an argv[1] to check
if (argc >2) {
//Check the option string
std::string option=std::string(argv[1]);
//create path "mean"
if (option == "mean") {
//declare a variable for the sum
//create a recursive addition to find the sum
int sum=0;
int avg=0;
while (argc>index) {
sum=sum+atoi(argv[index]); //recursively add to the sum
//increase index for recursion
index=index+1;
}
avg=sum/(argc-2); //Take the average
mainChoice=avg;
}

//create path "max"
else if ("max" == option) {
int max=atoi(argv[index]);
//Take the max
while (argc>index) {
//recursively increase the index
index=index+1;
if (atoi(argv[index]) > max) {
max=atoi(argv[index]);
}
}
mainChoice=max;
}

//create path "min"
else if ("min" == option) {
int min=atoi(argv[index]);
//Take the min
while (argc>index) {
//recursively increase the index
index=index+1;
if (atoi(argv[index]) < min) {
min=atoi(argv[index]);
}
}
mainChoice=min;
}

//create path "sum"
else if ("sum" == option) {
int sum=atoi(argv[index]);
while ((argc-1)>index) {
index=index+1;
sum=sum+atoi(argv[index]);
}
mainChoice=sum;
}
else {
return 1;
}
else if (2 > argc) {
//proper return value
return 2;
}
else if (2 == argc) {
//proper return value
return 3;
}
}
//display main message
std::cout<<mainChoice;
//Return 0 if everything ran successfully
return 0;
}

//84:2: error: 'cout' in namespace 'std' does not name a type
//86:2: error: expected unqualified-id before 'return'
//87:1: error: expected declaration before '}' token
//
Member
Posts: 3,580
Joined: Aug 17 2013
Gold: 275.01
Feb 10 2014 05:06pm
Missplaced brackets.

Code
mainChoice=sum;
}
else {
return 1;
}
[B]---> } <---- HERE[/B]
else if (2 > argc) {
//proper return value
return 2;


/e

My bad I meant you need to REMOVE the one just before your
std::cout << mainChoice;

Sorry ..

Good luck :)

This post was edited by SCVonSteroids on Feb 10 2014 05:07pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 10 2014 06:21pm
The problem is you can't have an else before an else if. Right in the middle of that huge if-elseif block there is a random else block statement that is returning 1. That needs to go at the end, or possibly your algorithm needs to be reworked.

On an unrelated note, your line comments state that you are doing "recursive additon" and "recursively increasing the index" yet you are not doing recursion at all. Did you need to use recursion for this assignment? Because if you did, I don't think you fully understand what recursion is.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll