Home / Software / Computer programming / help with C++ code
0 Members and 1 Guest 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
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.099 seconds with 20 queries.