Need help using char correctly in this program

I am learning to use the while loop in this program

Page 1 of 1

7 Replies - 1144 Views - Last Post: 14 August 2009 - 12:11 AM Rate Topic: -----

#1 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Need help using char correctly in this program

Posted 13 August 2009 - 03:48 PM

Ok the problem is I can not figure out how to use char correctly in this program. What the goal of this program is, To take the amount of four regions: North, South, East, West and display the sum of the monthly sales amounts. Use a while loop to allow the user to enter each of the four sales amounts, one at a time.

I got the program to work if the regions were displayed as 1, 2, 3, 4. Instead of the North, South and so on. I know I have to use char for storing words. But How do you write a counter while using char? so it only asks for those 4 regions and nothing else. Hope this makes sense.

Below is how I got it to work displaying the numbers. Can someone give me a hint as to how to start correctly with it displaying things as North, South, East, West. Thanks guys.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	// variables
	int regionGross = 0;
	int region = 1;
	int monthlyTotal = 0;

	while (region <=4)
	{
		cout << "Enter Region " << region << "'s monthly sales: ";
		cin >> regionGross;
		monthlyTotal = monthlyTotal + regionGross;

			region = region +1;
	} // end while

	cout << "Total monthly sales for the regions are: " << monthlyTotal << endl;
	
	return 0;
}   //end of main function



Is This A Good Question/Topic? 0
  • +

Replies To: Need help using char correctly in this program

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Need help using char correctly in this program

Posted 13 August 2009 - 04:01 PM

Here's a critical question: do you know what arrays are? Have you learned the syntax?

Furthermore, do you know what strings are? Have you learned about C++ strings (do you know about the string header)? What about C strings (array of characters)?
Was This Post Helpful? 0
  • +
  • -

#3 abazaid  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 09-June 09

Re: Need help using char correctly in this program

Posted 13 August 2009 - 04:05 PM

instead to use region as int variable u can change it to 2d array to store
regions as string:
 
char* region[4];
region[0]="North";
region[1]="East";
region[2]="South";
region[3]="West";
 

and the while loop:
int count=0;
while (count<4)
	{
		cout << "Enter Region " << region[count] << "'s monthly sales: ";
		cin >> regionGross;
		monthlyTotal = monthlyTotal + regionGross;

			count = count +1;  //or count++; it is easier
	} // end while


Was This Post Helpful? 1
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Need help using char correctly in this program

Posted 13 August 2009 - 04:14 PM

You shouldn't really be using char in C++ for 'words' (i.e. sequences of human readable text) - char is for single characters, or for old 'C' style code. C++ uses a type called string instead. One way you could do this would be an array of strings
#include <string>
#include <iostream>

int main()
{
    int i = 0;
    std::string region_names[] = 
        { "North" , "East" , "South" , "West" };

    while( i < 4 )
    {
        std::cout << region_names[i] << std::endl;
        ++i;
    }
} 
Care that arrays always start at index Zero!

This post has been edited by Bench: 13 August 2009 - 04:17 PM

Was This Post Helpful? 1
  • +
  • -

#5 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Need help using char correctly in this program

Posted 13 August 2009 - 04:37 PM

Bench can you explain what the ++i; does ?

I got the program to work correctly with your help with the string. I for some reason wasnt thinking about using a string because we havent used them since the beginning of the class. Also we get a generic blank file for each program and it always has the #include <iostream> at the top but nothing else and well to be honest its made me lazy doing things that way and I need to start from a blank slab and do it from now on. I have posted how I did the program. Please let me know if there is better ways for how I incorporated your help into what I already had. Thank you very much !!
#include <string>
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	int i = 0;
	int monthlyTotal = 0;
	int regionGross = 0;


	std::string regionNames[] = //Where the region listings are stored
		{ "North" , "East" , "South" , "West" };

	while( i < 4 )
	{
		std::cout << regionNames[i] << std::endl;
		++i;
		cin >> regionGross;
		monthlyTotal = monthlyTotal + regionGross;

	}// end while


	cout << "Total monthly sales for the regions are: " << monthlyTotal << endl;
	
	return 0;
}   //end of main function


This post has been edited by vwyodapink: 13 August 2009 - 04:40 PM

Was This Post Helpful? 0
  • +
  • -

#6 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Need help using char correctly in this program

Posted 13 August 2009 - 04:56 PM

Also on my next program I have to make one that will display a name with an entered pre defined ID number. How would you write a string to store the names that associate with an ID number? Or would that not be with a string at all and use if else instead? Sorry I am feeling lost in this chapter and things arnt flowing as easy as usual :P


I think i figured out the ++i; is it a different way of writing it for adding one more to the count?

This post has been edited by vwyodapink: 13 August 2009 - 04:58 PM

Was This Post Helpful? 0
  • +
  • -

#7 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: Need help using char correctly in this program

Posted 13 August 2009 - 09:51 PM

View Postvwyodapink, on 13 Aug, 2009 - 03:56 PM, said:

Also on my next program I have to make one that will display a name with an entered pre defined ID number. How would you write a string to store the names that associate with an ID number? Or would that not be with a string at all and use if else instead? Sorry I am feeling lost in this chapter and things arnt flowing as easy as usual :P


I think i figured out the ++i; is it a different way of writing it for adding one more to the count?

You can use strcmp() to compare the strings if they check, do {blah}. Also, here is code explaining the difference between '++i' and 'i++'. It is in C because I do not C++ cout and cin to well.
#include <stdio.h>

int main()
{
int i=0;

i++;
printf("%d", i);
return 0;
}


Will make i still equal to 0 but increment. While
#include <stdio.h>

int main()
{
int i=0;

++i;
printf("%d", i);
return 0;
}


Will print 1. This is not the best example but it was a try.

This post has been edited by redhotfire0: 13 August 2009 - 09:54 PM

Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Need help using char correctly in this program

Posted 14 August 2009 - 12:11 AM

View Postredhotfire0, on 14 Aug, 2009 - 04:51 AM, said:

You can use strcmp() to compare the strings if they check, do {blah}. Also, here is code explaining the difference between '++i' and 'i++'. It is in C because I do not C++ cout and cin to well.
You can compare C++ strings for equality using == (or inequality with !=) - in exactly the same way as with built-in types. i.e.
std::string str = "hello";
if( str == "hello" )
    std::cout <<  "goodbye"; 


View Postredhotfire0, on 14 Aug, 2009 - 04:51 AM, said:

i++;
printf("%d", i);


Will make i still equal to 0 but increment. While
++i;
printf("%d", i); 

Will print 1. This is not the best example but it was a try.

Both your examples show exactly the same result because the difference between pre-increment and post-increment are in the value they return, not in the value they store (the effect on 'i' is always to add one). To make your example work you need to direct the result of your ++i and i++ into the printf statement itself, i.e.
printf("%d", ++i);

and
printf("%d", i++); 

The first situation is a call to the pre-increment operator, which adds 1 to i and returns the new value to printf
the second situation is a call to the post-increment operator which creates a copy of i, increments i, and then returns the copy which still contains the original value.

This post has been edited by Bench: 14 August 2009 - 12:18 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1