Home / Software / Computer programming / help with C++ code
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: 1 2 [All] - (Bottom) Print
Author Topic: help with C++ code  (Read 1478 times)
EEVIAC
Guest
« on: April 22, 2010, 09:15:10 AM »

I don't know if this is possible to do, the way I'm trying it, but I'll explain the best I can..


Code: [Select]


#include <iostream>
using namespace std;
int main()
{
const char Size = 20;
    char FirstName[Size];



enum grades {A, B, C, D, E};
    grades TEST;


cout << "What is your first name? ";
    cin.getline(FirstName, Size);
   
   
cout << "What is your last name? ";
char LastName;
cin >> LastName;


cout << "What letter grade do you deserve? ";
int TEST;
cin >> TEST;
    Actual_Grade = TEST + 1;


cout << "What is your age? ";
int age;
cin >> age;



cout << "Name:" << LastName << "," << FirstName << "\n";
cout << "Grade:" << Actual_Grade << "\n";
cout << "Age:" << age << "\n";
   
return 0;


}




I would like the output to be like this:

What is your first name? John Doe
What is your last name? Hanncock
What letter grade do you deserve? A
What is your age? 20
Name: Hanncock, John Doe
Grade: B
Age: 20


Notice in the output, how the "A" grade is incremented to "B"...  Basically, the user does not end up getting the grade he wants...  A practice assignment in my book asks to write a program that resembles the output above...

I just can't figure out how to make the "A" increment to one lower, which happens to be "B", in this case..   As I said, I don't know if this is possible to do the way I'm trying, but this is the code I've got so far..

I can't get it to compile, as is...
IP logged
Vikram Varaprasad
Intermediate



Thanked: 6
Posts: 209




« Reply #1 on: April 22, 2010, 10:18:15 AM »

There is multiple declarations for variable TEST. You had already been declared it for enum type "grades" and also declared as an integer variable "int TEST" and you used the variable "Actual_Grade" without declaration.

Check them out.....
IP logged

Vikram...
EEVIAC
Guest
« Reply #2 on: April 22, 2010, 10:33:20 AM »

I made the changes..  It compiles but I cannot get the "A" to increment to "B"

The "A" never changes..

Everything else works..

The practice assignment calls for the output to be as I said above.  I'm wondering if I'm even using the correct approach to accomplish this.. I mean, using an enumerator.. I don't know.. Maybe I am but just not using the enumerator correctly.  I'm not sure if "TEST + 1" is a valid statement to use with an enumerator..
The assignment is vague.  It just says write a program that displays the above out put  :P
IP logged
Vikram Varaprasad
Intermediate



Thanked: 6
Posts: 209




« Reply #3 on: April 22, 2010, 10:42:15 AM »

Tell me what do you want to do exactly...... i can't get you. I mean why you are going to change A to B with out any necessary or conditions etc.....
IP logged

Vikram...
EEVIAC
Guest
« Reply #4 on: April 22, 2010, 10:53:35 AM »

If I can figure out the correct way (or one way) to increment the "A" to "B", as the output is shown, then I have accomplished the reason for doing this.  I'm just trying to learn something here.

Look again at the sample output...

What is your first name? John Doe
What is your last name? Hanncock
What letter grade do you deserve? A
What is your age? 20
Name: Hanncock, John Doe
Grade: B
Age: 20

The assignment says to write a program that displays THIS output, and I'm just trying to figure out how to do it.  The name, age, and grade preference can vary though..


The following is the output that I'm getting, when I run the program.  It was actually compiling before I mad the changes a few minutes ago, but it had "errors" when compiling... Obviously because I'm not doing something right here..

What is your first name? John Doe
What is your last name? Hanncock
What letter grade do you deserve? A
What is your age? 20
Name: Hanncock, John Doe
Grade: A
Age: 20

See the difference between the two ?  I can't get the initial "A" input, to increment to "B"  The program is suppose to do it, but I can't get the code right..
IP logged
Vikram Varaprasad
Intermediate



Thanked: 6
Posts: 209




« Reply #5 on: April 22, 2010, 11:06:28 AM »

Here is the code....

Code: [Select]
#include<iostream.h>
#include<conio.h>
void main()
{
char str[20],str1[20],grade;
int age;
clrscr();

cout<<"What is your first name?";
cin>>str;

cout<<"What is your last name?";
cin>>str1;

cout<<"What letter grade do you deserve?";
cin>>grade;

cout<<"What is your age?";
cin>>age;

grade=grade+1;

cout<<"Name :"<<str<<","<<str1<<"\n";
cout<<"Grade:"<<grade<<"\n";
cout<<"Age  :"<<age;

getch();
}
IP logged

Vikram...
EEVIAC
Guest
« Reply #6 on: April 22, 2010, 11:19:14 AM »

It looks nice  :)


I tried it though and it doesn't increment the A to B
IP logged
Vikram Varaprasad
Intermediate



Thanked: 6
Posts: 209




« Reply #7 on: April 22, 2010, 11:30:48 AM »

It works fine....
I ran it on Turbo C++ compiler

here is the output screen...

[recovering disk space - old attachment deleted by admin]
IP logged

Vikram...
EEVIAC
Guest
« Reply #8 on: April 22, 2010, 11:36:10 AM »

Interesting..

I keep getting build errors... It won't even compile now..
IP logged
Vikram Varaprasad
Intermediate



Thanked: 6
Posts: 209




« Reply #9 on: April 22, 2010, 11:41:53 AM »

Build errors ???

which compiler are you using ?
copy and paste the code as it is....

i ran it thrice for you, even it is very simple simple.......program.
IP logged

Vikram...
EEVIAC
Guest
« Reply #10 on: April 22, 2010, 11:46:38 AM »

I'm using MS Visual C++ 2008


yah, you did a good job  :)

I don't know what the deal is here...  I copy/pasted just as it is
IP logged
Vikram Varaprasad
Intermediate



Thanked: 6
Posts: 209




« Reply #11 on: April 22, 2010, 11:50:21 AM »

I'm unfamiliar with MS Visual C++.
but for those visual editors remove the lines
#include<conio.h>
and
getch();
IP logged

Vikram...
EEVIAC
Guest
« Reply #12 on: April 22, 2010, 11:53:43 AM »

still no go   


I'll look at the code more later, so I can really see how it works... I'm going to bed now..  ^-^


thx for you help
IP logged
Vikram Varaprasad
Intermediate



Thanked: 6
Posts: 209




« Reply #13 on: April 22, 2010, 01:32:00 PM »

I just installed MS Visual C++ 2008 express edition..... and found the faults of your program.
The previously given code is for console editors such as Turbo C++

try this....

1) open MS Visual C++ 2008
2) Click New --> project -->win32 --> win32 console application

    provide the name "win32console1" and save it

3) copy and paste the following code......

Code: [Select]
// win32console1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
char str[20],str1[20],grade;
int age;

printf("What is your first name?");
scanf("%s",&str);

printf("What is your last name?");
scanf("%s",&str1);

printf("What letter grade do you deserve?");
scanf("%c",&grade);         
        // "blindly used" as previously hitted enter can be directly taken as input for this scanf . Because I don't
        //  know how to clear the buffer of previous inputs...I wrote this additional line..( poor programming )
scanf("%c",&grade);           // this originally prompts for input

printf("What is your age?");
scanf("%d",&age);

grade=grade+1;

printf("Name : %s , %s \n", str,str1);
printf("Grade: %c \n",grade);
printf("Age  : %d \n",age);

return 0;
}



this is c code.... I found no "iostream" header file in visual C++. so i wrote in c.
IP logged

Vikram...
2x3i5x
Expert



Thanked: 116
Posts: 2,163

Computer: Specs
Experience: Familiar
OS: Windows Vista

« Reply #14 on: April 22, 2010, 02:12:33 PM »

I don't know what the deal is here...  I copy/pasted just as it is

EEVIAC, here is the C++ code you wanted below, it compiles fine with MS visual c++ express 2008 and it outputs exactly what you wanted.  :)

Code: [Select]
#include<iostream>
#include<string>
using namespace std;

void main()
{
string str, str1;
char grade;
int age;

cout<<"What is your first name? ";
getline (cin,str);

cout<<"What is your last name? ";
getline (cin,str1);

cout<<"What letter grade do you deserve? ";
cin>>grade;

cout<<"What is your age? ";
cin>>age;

grade=grade+1;

cout<<"\nName: "<<str1<<", "<<str<<"\n";
cout<<"Grade: "<<grade<<"\n";
cout<<"Age: "<<age<<"\n\n";
}


« Last Edit: April 22, 2010, 02:58:25 PM by 2x3i5x » IP logged
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,865

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #15 on: April 22, 2010, 06:38:46 PM »

The advantage of 2x3i5x's version is it is actually C++, not C pasted into C++.  :P
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
EEVIAC
Guest
« Reply #16 on: April 22, 2010, 07:50:57 PM »

Thanks, 2x3i5x   ;)
IP logged
EEVIAC
Guest
« Reply #17 on: April 23, 2010, 02:16:52 AM »

@ 2x3i5x


Just for the record, when you declare "grade" as a char type, does that mean entering "A" is really the ascii equivalent "65", and is incremented to "66" (B), when grade = grade + 1 ?     Is that how this is working ?


From what I understand, it's the cin object that converts character constants such as A to it's equivalent numeric value which is stored into ram, and when outputted, it's the cout object converts that numeric constant in ram back to the ascii equivalent..  hehe   I think I got this..  :P





IP logged
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,865

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #18 on: April 23, 2010, 02:27:20 AM »

EEVIAC: that's how it works with strings in every language, though- the characters are of course stored as their ASCII/ANSI/Unicode equivalent. The difference here is that C and C++ have no "byte" datatype- their "byte" is the char data type.

the i/o routines, and everything, treat it as a number. Nothing treats it like a string, or a string character.- however, when C or C++ send it to the Operating System display routines- the console treats it like an ASCII code- a byte being sent with 65 is represented with an A. this makes sense, in that printing 65 instead would indicate the writing of two, rather then one, byte. So we have it like this:

1. C/C++ program works with char data type (perhaps an array). it is treated, they get no special treatment- uppercasing is as easy as subtracting 32 in certain circumstances, and lowercase by adding 32.

2. at some point, the program sends the char data off to an output function.

3. the output function is explicitly designed to accept an array of bytes and translate them into display characters. note that this obviously doesn't apply for writing to a file- in that case the data is simply written to the file (if the file is later loaded by a text editor the various byte values are translated to the characters you see on-screen).
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
EEVIAC
Guest
« Reply #19 on: April 23, 2010, 02:34:55 AM »

ahh, illumination    8)



you get another Lolipop, BC
IP logged
EEVIAC
Guest
« Reply #20 on: April 23, 2010, 03:30:16 AM »

It's interesting that there are so many ways to accomplish a single task in programming.

The assignment that I was trying to do here wanted an output as specified in my previous posts.   I had to go back through the chapter to try to guess which way the author intended the program to be written.  I assumed the assignment was to be written using techniques discussed in it's corresponding chapter.  It didn't occur to me that I could have done it the way 2x3i5x suggested..  With out the instructor to go along with the book it's kind of difficult to figure out what's required.  In other words, I hope I learned what the author intended... 

What's really hard is trying to comprehend EVERYTHING that is discussed in the chapters, and recalling that information when you actually need it at the end of the chapter... For me, by the time I get through a chapter, sometimes I can't recall what was said at the beginning of the chapter...  :(          I wish I had photographic memory.  Programming is probably much easier certain people..
IP logged
2x3i5x
Expert



Thanked: 116
Posts: 2,163

Computer: Specs
Experience: Familiar
OS: Windows Vista

« Reply #21 on: April 23, 2010, 01:37:35 PM »

Yeah, there are different ways to do same thing. Some are more efficient methods than others and some are simpler to write out. So it takes practice to get things in place.  :P
IP logged
Pages: 1 2 [All] - (Top) Print 
Home / Software / Computer programming / help with C++ code « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.121 seconds with 19 queries.