I'm writing a c++ program for a class I'm taking. I'm having a problem getting the string variable into the void print_results function. Basically, at the beginning of the main function, I ask the user for their name. I can use the name variable in the main function and it works fine, but I have to display all the results in a void print_results function, and I'm not quite sure how to get a string type variable into that function. I'm using Visual Studio .net. I initialialize it in main-String *name. I've looked in my book, and notes, but no info on it. If anyone could help me out a bit, by telling me how to put strings into the function call, that'd be great.
Thanks in advance
Jay

Recommended Answers

All 6 Replies

#include <iostream>
using namespace std;

void Print();

int main()
{
   Print();
}

void Print()
{
     char Name[]
//Your code
}

If I understand what you asked right...

Hi,
It wud be really good if you can post your program. Are you getting problem when you are trying to pass the variable from main to print_results function ?

cheers,
aj.wh.ca

Actualy I read what you said over again. You should create a header file with all your declarations if you want to use it through out your program. Then include it to your .cpp file.

Well this is what I have for the program. Don't laugh, I'm new at this. I'm want to make a line in the output (void print_results) function to output the user's name, but I keep getting error messages.

/********************************************************************************
*FILENAME	:	Project.cpp										*
*PROGRAMMER	:	JMS													*
*PURPOSE	:	Compute and display commission rate and commission amt			*
*********************************************************************************/

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

//FUNCTION PROTOTYPES
double calc_comm_rate(char, double);
double calc_comm_amt(double, double);
void print_results(char, double, double, double, double);

int _tmain()
{
	String *name;
	char status;
	double sales, commRate, commAmt, salesMinusComm;



	Console::WriteLine("________________________________________________________________________________");
	Console::WriteLine("Hello, welcome to ACME Hammers Commission Calculator v1.0!\n");
	Console::WriteLine("________________________________________________________________________________");
	
	Console::WriteLine("Please enter your name: ");
	name = Console::ReadLine();
	
	Console::Write("Please enter your status (P)art time or (F)ull time (letter only): ");
	status = Convert::ToChar(Console::ReadLine());
	
	Console::Write("Please enter your sales: ");
	sales = Convert::ToDouble(Console::ReadLine());
	
	Console::WriteLine("\nThank you, please hold while I compute your information and");
	
	//function calls
	commRate = calc_comm_rate(status, sales);
	commAmt = calc_comm_amt(commRate, sales);
	salesMinusComm = sales - commAmt;
	print_results(status, sales, commRate, commAmt, salesMinusComm);

	return 0;
}//END MAIN

/********************************************************************************
*PURPOSE	:	Compute the commission rate										*
*PASSED IN	:	The employee's status and sales									*
*RETURNED	:	The commission rate												*
*********************************************************************************/

double calc_comm_rate(char status, double sales)
{

	double commRate;
	
	if (status == 'p' || status == 'P')
	commRate = .035;

	else if (status == 'f' && sales < 10000 || status == 'F' && sales < 10000)
	commRate = .05;

	else 
	commRate = .075;

	return commRate;
}//END CALC_COMM_RATE

/********************************************************************************
*PURPOSE	:	Compute the commission amount									*
*PASSED IN	:	The commission rate and amount of sales							*
*RETURNED	:	The amount of the commission									*
*********************************************************************************/
double calc_comm_amt(double commRate, double sales)

{
	double commAmt;

	commAmt = commRate * sales;

	return commAmt;

}//END CALC_COMM_AMT

/********************************************************************************
*PURPOSE	:	Display results													*
*PASSED IN	:	All user information and computed information					*
*RETURNED	:	Nothing															*
*********************************************************************************/

void print_results(char status, double sales, double commRate, double commAmt, double salesMinusComm)

{
	system("pause");
	system("cls");

	Console::WriteLine("Status: {0}\n", Convert::ToString(__box(status)));
	Console::WriteLine("Sales: {0}\n", sales.ToString("C2"));
	Console::WriteLine("Commission Rate: {0}\n", commRate.ToString("P1"));
	Console::WriteLine("Commission Amount: {0}\n", commAmt.ToString("C2"));
	Console::WriteLine("Sales Minus Commission: {0}\n", salesMinusComm.ToString("C2"));

	return;

}//END PRINT_RESULTS

Thank you again :D
Jay

Hi,
following shud work for you.

Modify the main function as

print_results(name, status, sales, commRate, commAmt, salesMinusComm);

instead of

print_results(status, sales, commRate, commAmt, salesMinusComm);

Change the print_results as follows.

void print_results(String*name, char status, double sales, double commRate, double commAmt, double salesMinusComm)

{
    system("pause");
    system("cls");

    Console::WriteLine("Report for: {0}\n", name );
    Console::WriteLine("Status: {0}\n", Convert::ToString(__box(status)));
    Console::WriteLine("Sales: {0}\n", sales.ToString("C2"));
    Console::WriteLine("Commission Rate: {0}\n", commRate.ToString("P1"));
    Console::WriteLine("Commission Amount: {0}\n", commAmt.ToString("C2"));
    Console::WriteLine("Sales Minus Commission: {0}\n", salesMinusComm.ToString("C2"));

    return;

}//END PRINT_RESULTS

Let me know if you face some issues.

cheers,
aj.wh.ca

Ok, got it working now. Thank you all for your reply's. I was actually forgetting to put the * it the original function prototype. Seems to work great now.
/bow
Thank's again :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.