6 Replies - 708 Views - Last Post: 12 March 2011 - 07:30 PM Rate Topic: -----

#1 Reef++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-March 11

basic calculator, trouble with arrays

Posted 12 March 2011 - 05:25 PM

Hey,

I heard that starting a basic calculator was a good way to practice C++ as you go, so I researched the starting points for the most basic one I could find, which just happened to be written by an author by the handle "ShawnCplus".

In this small section of code, I understand that char is a "Data Type", but don't understand what the [100], and everything thereafter means.

{
char name [100];
char quit;
quit ='\0';
while (quit !='q')
{


Thanks,

-Reef++

Is This A Good Question/Topic? 0
  • +

Replies To: basic calculator, trouble with arrays

#2 darek9576  Icon User is online

  • D.I.C Lover

Reputation: 196
  • View blog
  • Posts: 1,628
  • Joined: 13-March 10

Re: basic calculator, trouble with arrays

Posted 12 March 2011 - 05:38 PM

char name[100];



This refers to the array of 100 characters and you can refer to these characters using name[0] name[1] and so on.
Was This Post Helpful? 0
  • +
  • -

#3 Reef++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-March 11

Re: basic calculator, trouble with arrays

Posted 12 March 2011 - 05:58 PM

View Postdarek9576, on 12 March 2011 - 05:38 PM, said:

char name[100];



This refers to the array of 100 characters and you can refer to these characters using name[0] name[1] and so on.


I'm still not 100% sure on what point this section has in the entirety of the code block.
Here's the completed Calculator.

#include <iostream>
 
using namespace std;
 
int main()
{
char name [100];
char quit;
quit ='\0';
while (quit !='q')
{
system("title Average Calculator");
system("color 84");
int a, b, c, d, e, x, z; //loads integers into memory
cout <<"This is a simple average calculator. Just put in your 5 most important grades"<< endl;
cout<<"What is your name?"<< endl;
cin.getline (name,100);
cout<<"Hello, " << name << "."<< endl;
cout<<"Put in your first grade:"<< endl; //asks for grade
cin>> a; //Receives grade and loads it into memory
cout<<"Put in your second grade:"<< endl;
cin>> b;
cout<<"Put in your third grade:"<< endl;
cin>> c;
cout<<"Put in your fourth grade:"<< endl;
cin>> d;
cout<<"Finally, enter your last grade:"<< endl;
cin>> e;
z = a+b+c+d+e; //makes z integer equal to all grades added together (starting division equation)
x = z; //makes the x integer equal to z (to make me less frustrated)
if (x/5<=65) //Starting exceptions
{
cout<<"Are you even trying to do you're work!?"<< endl;
}
else if (x/5==75)
{
cout<<"Try to pick up the pace a little bit."<< endl;
}
else if (x/5==80)
{
cout<<"You are doing a decent job at keeping the grades up."<< endl;
}
else if (x/5==90)
{
cout<<"You're doing a great job, keep it up"<< endl;
}
else if (x/5>=95)
{
cout<<"Hmph. Teachers pet."<< endl; //End Exceptions
}
cout<<"Your average is " << x/5 << ".\a"<< endl; //Outputs final divisional answer (all grades/5)
cout<<""<< endl;
cout<<"Q - Quit"<< endl;
cout<<"R - Repeat"<< endl;
cout<<"Please type in the corresponding letter for your choice then press enter."<< endl;
cin>> quit;
cout<<"Goodbye, " << name << "."<< endl;
system("PAUSE");
system("cls");
}
 
return 0;
}



Now by removing lines: 06 - 11, you'll notice that nothing visually changes in the outcome of the compilation, but as I don't fully understand those lines, I can't test it appropriately.

-Reef++
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,635
  • Joined: 23-August 08

Re: basic calculator, trouble with arrays

Posted 12 March 2011 - 06:17 PM

For you to read. Also this on arrays.
Was This Post Helpful? 1
  • +
  • -

#5 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 76
  • View blog
  • Posts: 1,351
  • Joined: 28-March 09

Re: basic calculator, trouble with arrays

Posted 12 March 2011 - 06:40 PM

I would suggest you use argv[],argc to do a calculator so you could input the thing you want solved in one line or find another way to do it rather than 15 different entries.
Was This Post Helpful? 0
  • +
  • -

#6 Reef++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-March 11

Re: basic calculator, trouble with arrays

Posted 12 March 2011 - 07:29 PM

Thanks JackOfAllTrades, much appreciated.

Took me a while to work out what argv[] and argc meant, but I'll give it a shot, thanks IngeniousHax.

-Reef++
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4501
  • View blog
  • Posts: 24,965
  • Joined: 10-May 07

Re: basic calculator, trouble with arrays

Posted 12 March 2011 - 07:30 PM

** Renamed title to be more specific **
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1