infern0 24 Light Poster

I assume food and calories are parallel arrays meaning that food[x] has calories[x] calories and you want to keep the food matched with the correct calories and you are trying to alphabetize the food array. If that is the case then swap calories[x] and calories[x + 1} in the body if the same if. Likewise if you want to sort calories in ascending (or descending) order and you want to keep the food matched with the calorie value, then swap them in the body of the same if statement.

In your code what happens when i = 0 and c = x - i? Is c + 1 a valid index?

Thanks for your help.

infern0 24 Light Poster

Hi, I am having trouble getting the item and its calories to match. Could anyone tell me what is wrong with this bubble sort? Thanks

int main()
{
	string food[100];
	string search;
	int calories[100];
	int x = -1;
	bool look = false;
	do
	{
		x++;
		cout << "Enter a menu item (enter 'done' when finished): ";
		getline(cin, food[x]);
		if (food[x] != "done")
		{
			cout << "Enter the number of calories: ";
			cin >> calories[x];
			cin.ignore();
		}
	} 
	while (food[x] != "done");
	int i;
	int c;
	string tmp;
	//bubble sort
	for (i=0; i < x; i++)
	{
	for (c=0; c < x-i; c++)
		if (food[c+1]< food[c]) 
		{
			swap (food[c], food[c+1]);
		}
		if (calories[c+1] < calories[c])
		{
			swap (calories[c], calories[c+1]);
		}
	}
//binary search
	while (look == false)
	{
		int upper = x;
		cout << "Enter a product to look up: ";
		getline(cin, search);
		if (search == "done")
	{
		look = true;
		break;
	}
	int lower = 0;
	int pos;
	pos = ( lower + upper) / 2;
	while((food[pos] != search) && (lower <= upper))
	{
	if (food[pos] > search)
	{
		upper = pos - 1;
	}
	else
	{
		lower = pos + 1;
	}
	pos = (lower + upper) / 2;
	}
	if (food[pos] == search)
	{
		cout << food[pos] << " has " << calories[pos] << " calories." << endl;
	}
	if (lower > upper)
	{
	cout << search << " was not found." << endl;
	}
	}
}
infern0 24 Light Poster

solved

infern0 24 Light Poster

Here is the piece of code I am having problems with. It is supposed to let me look up any items I put in the program previously. If the user did not enter a product earlier it should say "product not found".

My problem is that it remembers the first product I entered into the program, but any other entries I made after that aren't recognized and say "product not found".

Please help.

bool look = false;
	do
	{
		cout << "Enter a product to look up: ";
		getline (cin, search);
		
		for (int y = 0; y < x; y++)
		{
		
			if (search == food[y])
			{
				cout << food[y] << " has " << calories[y] << " calories." << endl;
				look = true;
			}
			if ((look == false) && (search != "done"))
			{
				cout << search << " was not found." << endl;
				break;
			}
		}
	}
	while (search != "done");
}
infern0 24 Light Poster

Check the input immediately after reading it in and break out of the while loop right there if it's -1.

ETA: Or, rather, return immediately...I wasn't paying attention to where you do the calculations.

ETA2: You'll also have to make a flag to tell if you've read in any good values. Then, if(input == -1 AND goodValues == true) return;

I tried using a break with if statements with no luck...I came here as a last resort. Can anybody enlighten me?

infern0 24 Light Poster

I have to write a program that calculates the avg, min, and max. The program runs fine, but when a user decides he doesn't want to enter any scores (enter: -1) in the beginning it still tries to calculate the avg,min and max.

I need the program to end w/o trying to calculate the mean, etc if the user enters -1 for the first value.

Thanks in advance.

int main()
{
	double score[75];
	int x = -1;
	double total = 0;
	double min = 101;
	double max = -1;
	do
	{
		x++;
		cout << "Please enter a score (enter -1 to stop): ";
		cin >> score[x];

		if (score[x] < min)
		{
			if (score[x] != -1)
				min = score[x];
		}
		if (score[x] > max)
		{
			max = score[x];
		}	
	} 
	while (score[x] != -1);
	for (int y = 0; y < x; y++)
	{
		total += score[y];
	}
	cout << "Average is " << (total / (double)(x)) << endl;
	cout << "Highest is " << max << endl;
	cout << "Lowest is " << min << endl;
}
infern0 24 Light Poster

Thank you for the comments. Unfortunatly I have to do this program as I had listed with all the questions appearing at once.

After the first questions answer is satisfied only the remaining two questions should appear and so on.

Would you recommend breaking the order string down? If so how?

infern0 24 Light Poster

Let me see some of that sweet lovin' code..

int main ()
{
    string icecream = "", sauce = "", sprinkles = "";
    string order;

    do
    {
        if (icecream == "")
            cout << "Do you want chocolate, vanilla, or twist?" << endl;
        if (sauce == "")
            cout << "Hot fudge, chocolate or strawberry sauce?" << endl;
        if (sprinkles == "")
            cout << "Do you want sprinkles (yes/no)?" << endl;
        getline(cin, order);
        if ((order == "chocolate") || (order == "vanilla") || (order == "twist"))
            icecream = order;
		if ((order == "hot fudge") || (order == "chocolate") || (order == "strawberry"))
            sauce = order;
        if ((order == "yes") || (order == "no"))
            sprinkles = order;
    }
        while ((icecream == "") || (sauce == "") || (sprinkles == ""));
    if (sprinkles == "yes")
        cout << "You ordered " << icecream << " icecream with " << sauce << " sauce with sprinkles." << endl;
    else if (sprinkles == "no")
        cout << "You ordered " << icecream << " icecream with " << sauce << " sauce without sprinkles." << endl;
}
infern0 24 Light Poster

Hi, I'm having trouble writing a program that asks the user 3 questions.(3 possible answers for each) I am using a string to remember all the answers with a getline(cin,answer) statement.

My problem is the first two questions have choices for the answers that can be the same such as red, red.

The program works fine if the answers are all different, but when I put in "red" it automatically puts red for the first two answers and jumps to the 3rd question.

Any thoughts on how to get around this? I tried using a continute statement, but had no luck.

Thanks.

infern0 24 Light Poster

Yeah I changed something in my post before testing it and it actually doesn't work the way it should. Good job getting it done.

Thanks. I tend to make things more complicated then they actually are...

infern0 24 Light Poster

I was eating and working on the program. I already finished the program earlier. I was mixing up the input and the signs in the program...didn't need to switch from an int to a char after all :P It was simple after that. Thanks guys.

infern0 24 Light Poster

I have the program running now with all the operators working. I still cannot get the program to end on "X" and I am having trouble making it display a cout "Cannot divide by zero".

infern0 24 Light Poster

What errors?

(26) : error C2782: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<

etc, etc, I don't know what to make of it.

infern0 24 Light Poster

So take input as a string, and if it's not "X", then convert it to a number.

I don't know how to convert a string to a number.. I tried looking it up but it looks complicated.

Getting 10 errors in the build current. I think it's because of the input problem with the while statement.

infern0 24 Light Poster

Some starters: Sort out your confusion about strings and numbers. Assignment is = , comparison is == . Multiline blocks are not determined by indentation in this curly-brace language.

I was trying to use double to identify the input..but I can't use "X" because it is not a number.

infern0 24 Light Poster

I don't know how to use arrarys yet :(

infern0 24 Light Poster

Hi. I'm having trouble making a simple calculator.
The code I have so far compiles, but it does not run.
Right now I only have addition in because I wanted to make sure I was going in the right direction before I added anything else.
I have to use a Do...while statement in this code.
Can anyone tell me when the program doesn't run???
Here is what I have so far:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
	char sign;
	string input;
	string total=0;
	cout << "Current total is 0" << endl;
	do
	{	cout << "Enter an operation: + - * / (or enter X to exit):";
		cin >> sign;
		if (sign = '+')
			cout <<"Enter a number: ";
			cin >> input;
			cout <<"Current total is " << input + total;
	}
		while (input != "X");
}

Thanks in advance.

infern0 24 Light Poster

Ok. I started from scratch and I got it working. Thanks for the advice. You really helped me think about how the problem should be solved.

infern0 24 Light Poster

Still not solved. Anyone have any suggestions?

infern0 24 Light Poster

when is wrong is that you have too many cin's. YOu are making this a lot more difficult than it really is. All you need is one cin loop. You don't need any for loops in this program.

while( cin >> num)
{
   // do all calculations here
}
// now find the mean value and print everything.

BTW: In case you don't know, you have to press Ctrl+Z <Enter> to exit that loop.

So your saying I don't need all those brackets? Like this? I think the mean is working now...but there is a warning in the program and the range is still not working.

#include <iostream>
#include <string>
using namespace std;

int main ()
{
	double num, sum=0, min1=0, max1=0, range, count=0;

	cin >> num;
	
	while (cin >> num)
	{
		++count;
		sum += num;
		if (num < 0 || num > 100)
			cin.ignore();
		continue;
			
		
			
			cout << "out of range, value ignored" << endl;
		
	
		
		
			for (min1 = num; num > 0; )
			
				if (num < min1) min1 = num;
		
			
			for (max1 = num; num > 0; )
			
				if (num > max1) max1 = num;
				
			
		
		range = (max1 - min1);
	
	}
	double mean = (sum/count);
	cout << "The average is " << mean << endl;
	cout << "The range is " << range << endl;
}
infern0 24 Light Poster

Here much easier... =) This finds the sum of all the values between the range of values you enter.

#include <iostream>

using namespace std;

int main()

{
	long Sum = 0;
	long x, y;
	
	cout << "Enter the first #: ";
	cin >> x;
	cin.ignore();
	cout << "Enter the last #:  ";
	cin >> y;
    cin.ignore();
	// If the last number > then the first, inverse them

	if( x > y )
	{
		int Z;
		
		Z  = x;
		x = y;
		y  = Z;
	}
	
	for( int Counter = x; Counter <= y; Counter++ )
		Sum += Counter;

	cout << "Sum of numbers from " << x << " to " << y << " = "
         << Sum << endl;
    cout << "Press Enter to exit";
    cin.get();    
    return 0;
}

I don't think you read what I'm trying to do. I need to find the average and range of the numbers. I also need to be able to put in as many numbers as I want.

infern0 24 Light Poster

Average: sum of all the numbers entered divided by the quantity of numbers entered. So if I enter 5 numbers (1, 2, 3, 4 and 5) the average will be (1+2+3+4+5)/5. You need to do the same thing in your program. Just keep summing them up as you enter the numbers, keeping track of how many you entered. When all done entering the numbers then you can make the final calculation.

The range is found by keeping track of the largest value entered and the smallest value entered -- you need two variables to do that. When you enter a number check to see if it is smaller than the smallest value. If yes, then change the smallest value to be the same as the number just entered. Then check if the number just entered is greater than the largest number entered so far. If yes, then change the value of the greatest number to be the value of the number just entered. When you stop entering numbers you don't have to do anything more because its all done.

I understand how to find the mean and range...to me the program looks like it should work? I'm not understanding what went wrong?

#include <iostream>
#include <string>
using namespace std;

int main ()
{
	double num, sum=0, min1=0, max1=0, range, count=0;

	cin >> num;
	
	while (cin >> num)
	{
		++count;
		sum += num;
		if (num < 0 || num > 100)
		{	
			cout << "out of range, value ignored" << …
infern0 24 Light Poster

I'm have serious trouble with this program. The average and the range do not work. The program runs...I just don't know how to fix this. Please help.

I have to find the average and the range of an unlimited number of integers between 0 and 100.

#include <iostream>
using namespace std;

int main ()
{
	double num, sum=0, min1=0, max1=0, range, count=0;

	cin >> num;
	
	while (cin >> num)
	{
		++count;
		if (num < 0 || num > 100)
		{	
			cout << "out of range, value ignored" << endl;
		}
	
		else
		{
			for (min1 = num; num > 0; )
			{
				if (num < min1) min1 = num;
				{
					cin >> num;
				}
			}
			for (max1 = num; num > 0; )
			{
				if (num > max1) max1 = num;
				{
					cin >> num;
				}
			}
		
		sum += num;
		range = max1 - min1;
		}
	}
	cout << "The average is " << sum / count << endl;
	cout << "The range is " << range << endl;
}
}
infern0 24 Light Poster
#include <iostream>
#include <string>
using namespace std;

int main ()
{
	int num1, num2;
	long sum = 0;
	cout << "Please enter an integer between 0 and 100: ";
	cin >> num1;
	if (num1 >= 0 && num1 <= 100)
	{
		cout << "Please enter an integer between " << num1 << " and 100: ";
		cin >> num2;
	}
	else 
	{	
		cout << "Please read and follow the directions!";
	}
	if (num2 >= num1 && num2 <=100)
	{
		for (int i=num1; i<=num2; i++)
		{
			sum += i;
		}}
		else 
		{	
			cout << "Please read and follow the directions!";
		}
	cout << "The total of the integers between " << num1 << " and " << num2 << "is: " << sum;

}

do I have too many brackets? Still getting errors with your suggestion.

infern0 24 Light Poster

If think you are leaving out the return value.
Put

return 0;

before the final brace terminating the main() function.
Unless you didn't post your entire code...

The return 0 is still giving me errors :(

infern0 24 Light Poster

Ok, now I'm getting 2 errors. Do you see whats wrong?

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    int num1, num2;
    long sum = 0;
    cout << "Please enter an integer between 0 and 100: ";
    cin >> num1;
    if (num1 >= 0 && num1 <= 100)
    {
        cout << "Please enter an integer between " << num1 << " and 100: ";
        cin >> num2;
    }
    else 
    {   
        cout << "Please read and follow the directions!";
    }
    if (num2 >= num1 && num2 <=100)
    {
        while (num2 >= num1)
        {
            sum += num1++;
        }}
        else 
        {   
            cout << "Please read and follow the directions!";
        }
    cout << "The total of the integers between " << num1 << " and " << num2 << "is: " << sum;

}
Grn Xtrm commented: No code tags -- again +0
Ancient Dragon commented: No code tags -- again +24
infern0 24 Light Poster
/*
Write a program that reads two integers num1 and num2 from the user, then displays the total of the all
of the numbers between num1 and num2 (inclusive). Your program should handle the following:
1. Make sure the numbers entered are between 0 and 100.
2. Make sure the second number is greater than or equal to the first number entered.
*/

I'm getting one error: illegal use of else without match if statement. I can't seem to figure out whats wrong? Can anyone help?

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    int num1, num2;
    long sum = 0;
    cout << "Please enter an integer between 0 and 100: ";
    cin >> num1;
    if (num1 >= 0 && num1 <= 100)

        cout << "Please enter an integer between " << num1 << " and 100: ";
        cin >> num2;
        else cout << "Please read and follow the directions!";

    if (num2 >= num1 && num2 <=100)

        while (num2 >= num1)
        sum += num1++;
        else cout << "Please read and follow the directions!";

    cout << "The total of the integers between " << num1 << " and " << num2 << "is: " << sum;

}
infern0 24 Light Poster

1