C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 4th, 2008, 05:47 AM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
General - Help...build errors

Hi all,

I need help! I created an empty project and a cpps file. Then i complile it and try to run it. But i was prompt:
"There were build errors. Would you like to continue and run the last sucessful build" i click on Yes and was given the prompt: "Unable to start program 'C:document and settings/visual studio 2004/debug/TPP.exe. The system cannot find the file specified."

May i know how can i solve this?

Thanks

Reply With Quote
  #2  
Old March 4th, 2008, 05:53 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
Well if there are build errors there is probably no executable to run. So fix the build errors before you try to run it. You can post your source plus the compiler errors you are getting here and we can try to help you.
__________________
There is no such thing as C/C++, you either program C or C++

Reply With Quote
  #3  
Old March 4th, 2008, 09:47 AM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by Icon
Well if there are build errors there is probably no executable to run. So fix the build errors before you try to run it. You can post your source plus the compiler errors you are getting here and we can try to help you.


Hi thanks! The problem is solved. I guess something is wrong with my system. I retried it a few hrs later and its done! haha

Reply With Quote
  #4  
Old March 4th, 2008, 10:04 AM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
Oh dear! it happened again. This is the message i got!

------ Build started: Project: TMA Q1, Configuration: Debug Win32 ------
Linking...
TPP 1b.obj : error LNK2005: _main already defined in TPP.obj
TPP 1b.obj : error LNK2005: "int __cdecl main(void)" (?main@@$$HYAHXZ) already defined in TPP.obj
C:\Documents and Settings\Compaq_Owner\My Documents\Visual Studio 2005\Projects\TMA Q1\Debug\TMA Q1.exe : fatal error LNK1169: one or more multiply defined symbols found
Build log was saved at "file://c:\Documents and Settings\Compaq_Owner\My Documents\Visual Studio 2005\Projects\TMA Q1\TMA Q1\Debug\BuildLog.htm"
TMA Q1 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Reply With Quote
  #5  
Old March 4th, 2008, 10:08 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
It seems you have two main functions in your project. Probably one in 1b.cpp and one in TPP.cpp. You should remove one of them.

Reply With Quote
  #6  
Old March 4th, 2008, 11:42 AM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by Icon
It seems you have two main functions in your project. Probably one in 1b.cpp and one in TPP.cpp. You should remove one of them.


Thanks it works after removing one!

I am encountering another problem. I try to write a source code and run it.

My source code is:

#include <iostream>
using namespace std;

int main()
{
double x; y;
int quotient;

cout << "Enter the value for x : ";
cin >> x;

cout << "Enter the value for y : ";
cin >> y;

quotient = x/y;

cout << x + " " + y + " " + quotient ;

cin.ignore();
cin.ignore();
return 0;

But the prompt was given below:
------ Build started: Project: Q2a, Configuration: Debug Win32 ------
Compiling...
TPP 2a.cpp
.\TPP 2a.cpp(6) : error C2065: 'y' : undeclared identifier
Build log was saved at "file://c:\Documents and Settings\Compaq_Owner\My Documents\Visual Studio 2005\Projects\2a\2a\Debug\BuildLog.htm"
Q2a - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Where are my errors? Is there any syntax errors?

Reply With Quote
  #7  
Old March 4th, 2008, 12:07 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
Change
double x; y;
into
double x, y;

or maybe even into (i prefer this):
double x;
double y;

Reply With Quote
  #8  
Old March 4th, 2008, 12:18 PM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by Icon
Change
double x; y;
into
double x, y;

or maybe even into (i prefer this):
double x;
double y;


Done and i have the following message:
------ Build started: Project: Q2a, Configuration: Debug Win32 ------
Compiling...
TPP 2a.cpp
.\TPP 2a.cpp(15) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
Build log was saved at "file://c:\Documents and Settings\Compaq_Owner\My Documents\Visual Studio 2005\Projects\2a\2a\Debug\BuildLog.htm"
Q2a - 0 error(s), 1 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Why is there an error message? Is there a semantic error? If so, what is the cause of the error?

This program only run once. Is there any other way i can make it run repetitively until the use enters the same value for both the numbers, then terminate the program?

Sorry for troubling you. I am trying very hard to learn C++

Reply With Quote
  #9  
Old March 4th, 2008, 01:20 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
In this line:
quotient = x/y;
quotient is an int (whole number) but x, y, and x/y are doubles (i.e. floating point number). When you assign a double (or float) to an int you are 'losing' the digits after the comma in the conversion. Why don't you make quotient a double too?

As for the second question, look at the loop constructs in c++, i.e. 'for' and 'while' (and 'do .. while'). If you need help figuring it out just ask.

Good luck

Reply With Quote
  #10  
Old March 5th, 2008, 01:52 PM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
I am still trying to figure out how i can make use of loop. In the meanwhile, i started another exercise to do an investment plan i.e. investment amount will yield 8 percent interest annually. Number of years must not be ) or exceed 30 yrs.
They have an option to choose to continue : Y /N. Need to change lower case to upper case.

My codes are as below:
#include <iostream>
using namespace std;
int main()

{
int amount;
int years;
int annualreturn;


//Enter data for the two variables
cout <<"Enter an investment amount:";
cin >> amount;
cout <<"Enter a term in years:";
cin >> years;

if (years =0 || years >30);
cout <<"\Error:You must enter a value between 1 and 30"<< endl;
cout <<"Please reenter the term in years:"<< endl;
cin.ignore(); //--hold the screen
cin.ignore(); //--hold the screen
if (years>1 || years<30);
cout <<"After 1 year(s) the value is:";
cin>> amount*1.08
}

The error message was:

1>------ Build started: Project: tpp Q3, Configuration: Debug Win32 ------
1>Compiling...
1>tpp_q3.cpp
1>.\tpp_q3.cpp(18) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>.\tpp_q3.cpp(18) : warning C4129: 'E' : unrecognized character escape sequence
1>.\tpp_q3.cpp(23) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>.\tpp_q3.cpp(25) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion)
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\istream(1137): could be 'std::basic_istream<_Elem,_Traits> &std:perator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\istream(1139): or 'std::basic_istream<_Elem,_Traits> &std:perator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char &)'
11>.\tpp_q3.cpp(25) : error C2143: syntax error : missing ';' before '}'
1>Build log was saved at "file://c:\Users\danny\Documents\Visual Studio 2005\Projects\tpp Q3\tpp Q3\Debug\BuildLog.htm"
1>tpp Q3 - 2 error(s), 3 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I wanted to add in a while loop so that i can add on datas. But i am simply lost on how to start. Where should i place my do while? What should be included in the do while loop? Do i use switch to change the lowercase n to upper case N? How do i go about doing it?

Can someone show me how to get this programme running?Help....i dunno wat to do now!

Reply With Quote
  #11  
Old March 5th, 2008, 02:06 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
For your previous problem _A_ solution could be like this:
cpp Code:
Original - cpp Code
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(int argc, char* argv[])
  6. {
  7.    double x;
  8.    double y;
  9.    double quotient;
  10.  
  11.    do
  12.    {
  13.       cout << "Enter the value for x : ";
  14.       cin >> x;
  15.  
  16.       cout << "Enter the value for y : ";
  17.       cin >> y;
  18.  
  19.       quotient = x/y;
  20.  
  21.       cout << x << " / " << y << " = " << quotient << endl;
  22.    }
  23.    while( x != y ); // Note: comparing doubles (or floats) like this is tricky
  24.  
  25.    system("pause");
  26.  
  27.    return 0;
  28. }
This is by no means a good program, there is no input validation and comparing doubles like that is also not right. But it does give you an idea of a while loop. I will look at your current question later when I have more time.

Reply With Quote
  #12  
Old March 5th, 2008, 02:15 PM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
[Thank you very much! I really appreciate the help that you have given. I wil try to learn as much as possible. WIll be trying out other exercises so that i can be more familiar with it.

Reply With Quote
  #13  
Old March 5th, 2008, 03:32 PM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
I am doing another exercise whereby i need to write an application that calculates the amt of money earned on an investment based on an 8 percent annual return. The program prompts the users to enter an investment amount and the no of yrs for the investment. The valid values for the no of years range from 1 to 30. The program displays the ttl amount for each year of the investment. The console program reads input from the keyboard and the enrty continues until the user enters "N" which denotes end of data entry. The program is expected to use a character library function (toupper) to change 'n' to 'N" to the same case.

I tried a few attempts but still cant seemed to get it right
#include <iostream>
using namespace std;
int main(int,double)

{
int amount;
int years;
double annual;

do
{
//Enter data for the two variables
cout <<"Enter an investment amount:"<<endl;
cin >> amount;
cout <<"Enter a term in years:"<<endl;
cin >> years;
if (years< 1 || years> 30)
{
cout<<"\nYou must enter a value between 1 and 30"<<endl;
cout<<"Please re-enter the terms in years:"<<endl;
cin.ignore(); //--hold the screen
cin.ignore(); //--hold the screen
system("cls");
}
}while (years <1 || years > 30);

switch (years)
{
case 3 : cout<<"Enter an investment amount:"<<endl;
cin>> amount;
cout<<"Enter the terms in years"<<endl;
cin>> years;
annual=amount*years*0.08;
cout<<"After one year(s) the value is:"<<endl;
break;
}
case 2: case 1 :cout<<"Enter an investment amount:"<<endl;
cin>> amount;
;cout<<"Enter the terms in years"<<endl;
cin>> years;
annual=amount*1.08;
cout<<"After one year(s) the value is:"<<endl;
annual=(amount*1.08)*1.08;
cout<<"After two year(s) the value is:"<<endl;

Message:1>------ Build started: Project: tpp Q3, Configuration: Debug Win32 ------
1>Compiling...
1>tpp_q3.cpp
1>.\tpp_q3.cpp(37) : error C2046: illegal case
1>.\tpp_q3.cpp(37) : error C2046: illegal case
1>.\tpp_q3.cpp(48) : fatal error C1075: end of file found before the left brace '{' at '.\tpp_q3.cpp(5)' was matched
1>Build log was saved at "file://c:\Users\danny\Documents\Visual Studio 2005\Projects\tpp Q3\tpp Q3\Debug\BuildLog.htm"
1>tpp Q3 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

#include <iostream>
using namespace std;
int main(int,double)

{
int amount;
int years;
double annual;

do
{
//Enter data for the two variables
cout <<"Enter an investment amount:"<<endl;
cin >> amount;
cout <<"Enter a term in years:"<<endl;
cin >> years;
annual=amount*1.08;
cout<<"After one year(s) the value is:"<<endl;
}while (years >1||years<30);

do
{
cout << "Error:You must enter a value between 1 and 30" << endl;
cout << "Please reenter the term in years:" << endl;
cin.ignore(); //--hold the screen
cin.ignore(); //--hold the screen
}while(years < 1 || years > 30);

I do not understand where i went wrong. Can you please teach me how to solve? I am stuck and cant seemed to be able to proceed.

Reply With Quote
  #14  
Old March 5th, 2008, 04:34 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
You could do something like this:
cpp Code:
Original - cpp Code
  1. #include <iostream>
  2. #include <cctype>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8.    bool haveToContinue = true;
  9.  
  10.    while( haveToContinue )
  11.    {
  12.       double amount;
  13.       int nrYears;
  14.      
  15.       // get input     
  16.       cout << "Enter the investment amount: ";
  17.       cin >> amount; // no input checking..
  18.  
  19.       cout << "Enter the number of years: ";
  20.       cin >> nrYears;
  21.       while( nrYears < 1 || nrYears > 30 )
  22.       {
  23.          cout << "The number of years has to be in the range of 1 to 30 years.\n";
  24.          cout << "Enter the number of years: ";
  25.          cin >> nrYears;
  26.       }
  27.  
  28.       // calculate investment
  29.       for( int y = 0; y < nrYears; ++y )
  30.       {
  31.          amount *= 1.08;
  32.          cout << "After " << (y+1) << " year(s) the total is: " << amount << endl;
  33.       }
  34.  
  35.       cout << "\nDo you want to continue? Y/N ";
  36.       char answer;
  37.       cin >> answer;
  38.       if( toupper(answer) == 'N' )
  39.          haveToContinue = false;
  40.    }
  41.  
  42.    return 0;
  43. }
It really pays off to do it for yourself though. Is this a homework assignment? cin.ignore() does not necessarily 'hold the screen' by the way. The main routine does not take an int and a double. Good luck

Reply With Quote
  #15  
Old March 5th, 2008, 04:44 PM
darken4life darken4life is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 159 darken4life User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 28 m 57 sec
Reputation Power: 12
First off why do you have:
int main(int,double)

Also, I hope you aren't doing 30 switch cases, you can pass the # of years in a function and since you know annual return is 8 percent.

*Edit: Looks like someone beat me to it ^^

Reply With Quote
  #16  
Old March 6th, 2008, 02:03 AM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
Thanks. I will study it again. Nope this is not a homework assignment. These are some past year papers questions where i do not have answers to it and would like to practise. But i don't seem to be able to figure out the gist of it.

Reply With Quote
  #17  
Old March 6th, 2008, 02:04 AM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by darken4life
First off why do you have:
int main(int,double)

Also, I hope you aren't doing 30 switch cases, you can pass the # of years in a function and since you know annual return is 8 percent.

*Edit: Looks like someone beat me to it ^^


Hi, in fact if no one mention about it, i really have the intention to do so as i am very confused now. SOB!

Reply With Quote
  #18  
Old March 6th, 2008, 03:01 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
I am not sure I understand the question this time. You want to make a function instead of a huge switch?

Reply With Quote
  #19  
Old March 6th, 2008, 03:22 AM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by Icon
I am not sure I understand the question this time. You want to make a function instead of a huge switch?


For that question, i was thinking of doing a switch but had no idea how to...so i thot i could do it case by case from 1 to 30. I am studying your code and to help myself..i will try to do a software development procedures. You think this is a good idea?

Reply With Quote
  #20  
Old March 6th, 2008, 03:26 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
Making a switch statement for the 1 to 30 cases would work but it a lot of code would be repeated. This means that it is more difficult to maitain and becomes more error prone. If you use a loop like darken4life described (in a function or not) then you can change the range (1 to 100 for all you care) without having to change much code.

Software development procedures are nice to study but are not that relevant to a simple code question like this I think.

Reply With Quote
  #21  
Old March 6th, 2008, 05:26 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 13
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
do ... while
Code:
do {
  //a lot of times, at least one
} while (condition);


while ...
Code:
while (condition) {
  // a lot of times, or maybe not at all
}


for
Code:
for (initialization; condition; for_every) {
  // a lot of times
}
//Relevant example:
for (int i=1; i<=years; ++i) {
  amount *= 1.08;
  cout << "After year " << i << " the amount is " << amount << endl;
}

Reply With Quote
  #22  
Old March 6th, 2008, 01:41 PM
darken4life darken4life is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 159 darken4life User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 28 m 57 sec
Reputation Power: 12
any code that requires massive coding should always be a loop or a function.
30 switch cases is alot of code for a simple program, imagine somone wants to calculate 60 years or even 80 years, you going to write 80 switch cases? ^_^.
Like Mahuja wrote, use a for loop cause you know each year is always times 8 percent return.

Reply With Quote
  #23  
Old March 7th, 2008, 12:58 AM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by darken4life
any code that requires massive coding should always be a loop or a function.
30 switch cases is alot of code for a simple program, imagine somone wants to calculate 60 years or even 80 years, you going to write 80 switch cases? ^_^.
Like Mahuja wrote, use a for loop cause you know each year is always times 8 percent return.


Hi,

Thanks for alerting me. Would try to learn as much as possible!

Reply With Quote
  #24  
Old March 7th, 2008, 12:59 AM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by MaHuJa
do ... while
Code:
do {
  //a lot of times, at least one
} while (condition);


while ...
Code:
while (condition) {
  // a lot of times, or maybe not at all
}


for
Code:
for (initialization; condition; for_every) {
  // a lot of times
}
//Relevant example:
for (int i=1; i<=years; ++i) {
  amount *= 1.08;
  cout << "After year " << i << " the amount is " << amount << endl;
}


Thank you for taking the time to explain. Really appreciate it!

There are so many nice people here!

Reply With Quote
  #25  
Old March 7th, 2008, 01:05 AM
danteamb danteamb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 14 danteamb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 0
if i am supposed to create an Purchase type which includes

Invoice ID: 1000
Sales amount: 200.50
Discount: 7.5%

(a) Write down a C++ record type, purchaseType for the above information. Write
down the C++ declaration of a variable, purchaseTable to hold a table of 20 such
records.

struct purchaseType
{
int iinvoiceID;
float fsalesAmount;
float discount; // in %
} ;
purchaseType purchaseTable [20] ;
(b) Write C++ statements to assign the last element of purchaseTable with the
following data:
InvoiceID : 1004
Sale Amount : 450.00
Discount : 6%

purchaseTable [19].iinvoiceID = 1004 ;
purchaseTable [19].fsalesAmount = 450.00 ;
purchaseTable [19].discount = 6 ;
(c) Write a function, getInvoice that takes in one argument, InvoiceID and returns the
index of the invoice if it is in the list, otherwise it returns -1.

static int getInvoice (int id)
{
for (int i = 0 ; i<sizeof(purchaseTable)/sizeof(purchaseTable[0]); i++)
{
if (purchaseTable[i].iinvoiceID == id)
{
return i ;
}
}
return -1 ;
}

(d) Write a main program that allows the user to enter the InvoiceID and called the
function getInvoice to find if it is in the list. When it is found, the program
calculates the final sales amount using the following formula:

final Sales Amount = (1-Discount/100) * final Sales Amount
final Sales Amount=(1-Discount/100)*Sales Amount

If the InvoiceID is valid, the invoice details and the final sales amount are
displayed. If the invoiceID is not in the list, write out appropriate message to
inform the user. When the user enters S, the program terminates.

#include "stdafx.h"

#include <iostream>
#include <fstream>

using namespace std;

struct purchaseType
{
int iinvoiceID;
float fsalesAmount;
float discount; // in %
} ;

purchaseType purchaseTable [20] ;

static int getInvoice (int id) ;

int _tmain(int argc, _TCHAR* argv[])
{
purchaseTable [19].iinvoiceID = 1004 ;
purchaseTable [19].fsalesAmount = 450.00 ;
purchaseTable [19].discount = 6 ;

int id ;

while (1)
{
cout << "Enter Invoice ID (enter Ctrl-C to exit): " ;
cin >> id;
fflush(stdin) ;

if (id < 0)
{
cout << "Less than 0" << endl;
return 0 ;
}

int idx = getInvoice (id) ;

if (0 <= idx )
{
float finalSalesAmount=(1-purchaseTable [idx].discount/100)*purchaseTable [idx].fsalesAmount ;
cout << "Invoice ID: " << id << endl;
cout << "Sales Amount: " << "$" << purchaseTable [idx].fsalesAmount << endl;
cout << "Discount: " << purchaseTable [idx].discount << "%" << endl;
cout << "Final Sales Amount: " << "$" << finalSalesAmount << endl;

}
else
{
cout << "The invoice ID is not found." << endl ;
}

}


return 0;
}


static int getInvoice (int id)
{
for (int i = 0 ; i < sizeof (purchaseTable)/sizeof (purchaseTable[0]); i++)
{
if (purchaseTable[i].iinvoiceID == id)
{
return i ;
}
}
return -1 ;
}

Are these correct?

Reply With Quote
  #26  
Old March 7th, 2008, 08:51 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 13
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
int _tmain(int argc, _TCHAR* argv[])
Non-portable.

fflush(stdin);
Has a different effect from what you're expecting. You could use cin.flush() - but its behaviour is (by what I remember) implementation-dependent, i.e. nonportable.
Consider using cin.ignore(10,'\n') - 10 being the highest number of characters that will be discarded.

The use of endl isn't incorrect, but << '\n' suffices if your next command is outputting too... Or even put the endline character at the beginning of the next string.
Code:
cout << "Invoice ID: " << id 
  << "\nSales Amount: " << "$" << purchaseTable [idx].fsalesAmount << endl;

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Help...build errors


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap