C++ Assignment 4.1 & 4.7

Moderators: vinyard, KreepingMonkey

Message
Author
User avatar
zero86ea
C.S. Server Admin
Posts: 318
Joined: Tue Jun 19, 2007 12:02 am
Contact:

C++ Assignment 4.1 & 4.7

#1 Post by zero86ea » Sat Sep 29, 2007 2:51 pm

This is project 4.1 and i wanna see if you guys can do it!
Write a C++ program that requests and displays information as shown in the following
example of output:

What is your first name? Betty Sue
What is your last name? Yew
What letter grade do you deserve? B
Name: Yew, Betty Sue
Grade: C
Age:22


Note that tthe program should be able to accept first names that comprise more than one
word. Also note that the program adjusts the grade downward-that is, up one letter .
Assume that the user requests A, a B, or a C so that you don't have to worry about the
gap between a D and an F.
well i wanna see you try it so i can sort of understand this a little more!
thx :)
Last edited by vinyard on Mon Oct 01, 2007 10:36 pm, edited 1 time in total.
Reason: Updated thread title.
Image

User avatar
GARK
News Guy
Posts: 67
Joined: Fri Jun 15, 2007 9:54 pm

Re: C++ Assignment 4.1

#2 Post by GARK » Sat Sep 29, 2007 4:51 pm

Look for an example program in the book that does something like it, then modify it and make it your own. rinse and repeat. I'm personally starting off with 4.7, because it looks harder. If it helps, write 1 program for each line of output in your program for starters. Example

First write a program for "What is your first name? Betty Sue". Make it to work.

Write a separate program for "What is your last name? Yew". Make it work.

Then a program for "What letter grade do you deserve?". etc...

Don't try to do the whole program at once if you don't understand it. Break it down to the smaller pieces first, then combine it all. It's easier to help you if you ask about one part of the program and not the whole thing at once. Good luck! ;)

User avatar
zero86ea
C.S. Server Admin
Posts: 318
Joined: Tue Jun 19, 2007 12:02 am
Contact:

Re: C++ Assignment 4.1

#3 Post by zero86ea » Sat Sep 29, 2007 5:10 pm

well i did the whole thing and when i run it, i get the first message: What is your first name?,
then i type any name in, and the command prompt closes...don't know why.
but im gonna do what you said, do one at a time and i think it just might work! thx alot man :)
Image

User avatar
GARK
News Guy
Posts: 67
Joined: Fri Jun 15, 2007 9:54 pm

Re: C++ Assignment 4.1

#4 Post by GARK » Sat Sep 29, 2007 5:54 pm

What Programming Environment are you using? Visual Studio 2005 or Bloodshed Dev C++? Try adding the line:

system("pause");

That should prevent the command prompt from closing until you hit the Enter key.
Insert it before the return statement like this:

Code: Select all

#include <iostream>
using namespace std;

int main()
{
 
    cout << "Hello World!\n";   
    
    system("pause");    // pause screen
    return 0;    
}

User avatar
zero86ea
C.S. Server Admin
Posts: 318
Joined: Tue Jun 19, 2007 12:02 am
Contact:

Re: C++ Assignment 4.1

#5 Post by zero86ea » Sat Sep 29, 2007 5:55 pm

man this sucks! after i finish, i run the program and enter any name, but then it shows me
the name i entered is the number "2" don't know why :(
Image

User avatar
zero86ea
C.S. Server Admin
Posts: 318
Joined: Tue Jun 19, 2007 12:02 am
Contact:

Re: C++ Assignment 4.1

#6 Post by zero86ea » Sat Sep 29, 2007 5:56 pm

well...this is what i got so far! oh and im using bloodshed dev C++

Code: Select all

#include <iostream>
#include <cstdlib>

using namespace std;

int main ()                          //This is the main function
{
    int firstName;
    int lastName;
    int grade;
    int age;
    
    cout << "What is your first name?";
    cin >> firstName;
    cout << "What is your last name?";
    cin >> lastName;
    cout << "What letter grade do you deserve?";
    cin >> grade;
    cout << endl;
    cout << "What is your age?";
    cin >> age;
    
    cout << "Name: " << lastName << ", " << firstName; 
    cout << grade;
    cout << age;
    system("PAUSE");
    return 0;
}
Try running it and see what happens :(
I'm a rookie at this so please don't make fun of my mistakes :(
Thx a lot gark :)
Image

User avatar
GARK
News Guy
Posts: 67
Joined: Fri Jun 15, 2007 9:54 pm

Re: C++ Assignment 4.1

#7 Post by GARK » Sat Sep 29, 2007 7:10 pm

Okay! So you haven't been reading the book.
First, you have to include the library called "string" because you need to take in strings from the keyboard, NOT just "int".
Add this: #include <string> // add string library

And these:
int firstName;
int lastName;
int grade;

Should be this:

string firstName; // store two first and middle name
string lastName; // store last name
char grade; // store one character

Just look at the programming examples in the book on "Strings". We want to see your results man!

User avatar
zero86ea
C.S. Server Admin
Posts: 318
Joined: Tue Jun 19, 2007 12:02 am
Contact:

Re: C++ Assignment 4.1

#8 Post by zero86ea » Sat Sep 29, 2007 7:39 pm

Damn...thx alot gark :) i got it to work! finally!
Well here are the results...still need to fix the firstName part cause of the space.

Code: Select all

//Programmer  : Efren Arrieta
//Class       : Co Sci 440 C++ 
//Excercise   : Excercise 4.1 Page 174

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main ()                                                 //This is the main function
{                                                           
    string firstName;                                       //First and middle name
    string lastName;                                        //Last name
    string age;                                             //Age
    char grade;                                             //Grade deserved
                                                            
    
    cout << "What is your first name?";                     //Displays First and middle name
    cin >> firstName;                                       //Enter first name
    cout << "What is your last name?";                      //Displays last name
    cin >> lastName;                                        //Enter last name
    cout << "What letter grade do you deserve?";            //Displays grade deserved
    cin >> grade;                                           //Enter grade deserved
    cout << "What is your age?";                            //Displays age
    cin >> age;                                             //Enter age
    
    cout << "Name: " << lastName << ", " << firstName;      //Displays last name first, then first and middle name
    cout << endl;
    cout << "Grade: " << grade;                             //Displays final grade
    cout << endl;
    cout << "Age: " << age;                                 //Displays final age
    cout << endl;
    system("PAUSE");                                        
    return 0;
}
Image

User avatar
zero86ea
C.S. Server Admin
Posts: 318
Joined: Tue Jun 19, 2007 12:02 am
Contact:

Re: C++ Assignment 4.1

#9 Post by zero86ea » Sat Sep 29, 2007 7:40 pm

well guys i gotta go but ill be back about 1 or 2...or maybe 3am :)
hope you guys are still online!

OH GARK THANKS MAN! SEE YOU IN CLASS :)
Image

User avatar
vinyard
Server Admin
Posts: 301
Joined: Fri Jun 15, 2007 7:43 pm
Operating System: Windows Vista Ultimate 32bit
CPU: Intel C2D E8200 [Stock]
Video Card: EVGA GeForce 8800 GT 512MB SC
Sound Card: Onboard
Memory: Corsair Twin2x2048-6400Pro
Motherboard: EVGA 680i AR1 p32
Contact:

Re: C++ Assignment 4.1

#10 Post by vinyard » Sun Sep 30, 2007 12:43 am

Here is mine:

Code: Select all

//Assignment 4.1
//Code By: David Rivera
//Date: 09.29.2007

#include <iostream>

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

char abuseGrade(char grade);
const int length = 30;                                          //Names greater than 30 characters are not allowed.

int main()
{
    char firstName[length];											//user first name (max 29 characters)
    char lastName[length];											//user last name (max 29 characters)
    char grade;														//user grade
    int age;														//Users age
   
	/* Promt the user for unformation */
    cout << "What is your first name?";                             //Ask for user's first name
    cin.get(firstName, length).get();                               //Get the user's first name & middle name if any
    cout << "What is your last name?";                              //Ask for user's last name
    cin >> lastName;                                                //Get user's last name
    cout << "What letter grade do you deserve?";                    //ask for user input
    cin >> grade;                                                   //Get user's grade
    cout << "What is your age?";                                    //Ask for user's age
    cin >> age;                                                     //Get users age

	/* Abuse the grade the user inputed */
	grade = abuseGrade(grade);
   
    /* Output user information */
    cout << "Name: " << lastName << ", " << firstName << endl;      //Print name
    cout << "Grade: " << grade << endl;                             //Print grade
    cout << "Age: " << age << endl;                                 //Pring age
   
    system("PAUSE");                                                //Wait for user to press a key
    return 0;
}


/* This function abuses the grade the user inputed & gives him a grade lower */
char abuseGrade(char grade)
{
	char g;															//declare g (modified grade)
	switch(grade)													//start a switch statement to compare user inputed grade
	{
	case 'A': case 'a':												//If 'A' || 'a'
		g = 'B';													//return 'B'
		break;
	case 'B': case 'b':												//If 'B' || 'b'
		g = 'C';													//return 'C'
		break;
	case 'C': case 'c':												//If 'C' || 'c'
		g = 'D';													//return 'D'
		break;
	case 'D': case 'd':												//If 'D' || 'd'
		g = 'F';													//return 'F'
	}
	return g;														//Return the modified grade
}
Assignment 4.7

Code: Select all

//Assignment 4.7
//Code by: David Rivera
//10.01.2007

#include <iostream>

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

const int length = 30;												//maximum length of characters for company name 

/* variable struct (template) */
struct companyInfo
{
    char companyName[length];										//declare company name variable
    double pizzaDiameter;											//declare pizza diameter variable
    int pizzaWeight;												//declare pizza weight variable
};

int main()
{

	/* Promt user for information */
	companyInfo company;											//create struct comapny
    cout << "Enter the Pizza Company Name: ";						//ask for company name
    cin.get(company.companyName, length).get();						//Get comapny name
    cout << "Enter the pizza diameter in inches: ";					//Ask for pizza diameter
    cin >> company.pizzaDiameter;									//Get pizza diameter
    cout << "Enter the pizza weight in pounds: ";					//ask for pizza weight
    cin >> company.pizzaWeight;										//get pizza weight

	/* output user inputed info */
    cout << "Company: " << company.companyName << endl;				//Output company name
    cout << "Pizza Diameter: " << company.pizzaDiameter << endl;	//Output pizza diameter
    cout << "Pizza weight: " << company.pizzaWeight << endl;		//Output pizza weight
    system("PAUSE");												//pause until user presses a key

  return 0;
}
										/* PROGRAM END */
Last edited by vinyard on Mon Oct 01, 2007 10:37 pm, edited 4 times in total.
Reason: Updated 4.1 Code, its now correct & 100% complete. 4.7 Updated. Done.
- vinyard.

Post Reply