My Advanced c++ instructor is really confusing me.

Ok, now I'm taking an advanced c++ course where we are supposed to convert to a standard that c and c# can use as well as c++. Here is the code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    // Lab 1, Programming Conventions, to read and print two sets of inputs, with two calculated values.
  // Programmer:  Worlds worst programmer
  // Editor(s) used: XP Notepad 
  // Compiler(s) used: VC++ 2010 Express

  // preprocessor directive for streaming, variable, and ending statement.
  #include <iostream>
  using std::cin;
  using std::cout;
  using std::endl;

  // preprocessor directive for string, and variable string.
  #include <string>
  using std::string; 
  using std::getline;

  //preprocessor for the decimal placement.
  #include <iomanip> 
  using std::ios;
  using std::setprecision;

  #include <cstdlib>

int main()

{
  //Jesse Burns program number 1: the basics.
  cout << "Lab 1, Programming Basics" <<endl;
  cout << "Programmer: Worlds worst programmer" <<endl;
  cout << "Editor(s) used: XP Notepad" <<endl;
  cout << "Compiler(s) used: VC++ 2010 Express" <<endl;

  // The variables needed for program
  int age;
  int ftemp;
  double ctemp;
  string name;
  string city;
  string buf;

  //The printed output and input for variables needed to gather the information
  cout << "Enter your age: "<<endl;
  cin >> buf; age = atoi(buf.c_str());
  cin.ignore(1000,10);
  cout << "Enter your name: "<<endl;
  getline (cin, name);
  cout << "Enter the temperature outside right now (degrees F): "<<endl;
  cin >> buf; ftemp = atoi(buf.c_str());
  cin.ignore(1000,10);
  cout << "What city are you in right now: "<<endl;
  getline (cin, city);
  
  //The conversion process needed or the math formulas and proper decimal indentation
  ctemp = (5.0 / 9.0) * (ftemp - 32.0);
  cout.setf(ios::fixed|ios::showpoint); 
  cout << setprecision(1);

  //The printed output
  cout << name << " is " << age << " years old now, and will be "<< age-- <<" a year from now.";
  cout << endl << "It's " << ftemp << " degrees in " << city << " -- that's "<<ctemp<< " degrees c."<<endl;
  cout << "Press enter to continue..."<<endl;

  cin.get();
}


And here is his response below.

Better (lab 1) but I get this because you used atoi instead of atof (-2):

Enter the temperature outside right now (degrees F): 45.67
It's 45 degrees in...

Think about what you should have done in your testing before submitting this, because you should have found this logic error before I did. Make sure you know all the requirements for lab 1, and fix and resubmit it.


Arggghhghgh! I don't get it. I thought atof was used in string literal cases like cin(get). What is his problem? Thanks guys/girls.
What is his problem?

It appears that he did like the fact that your program ignored the .67

(atoi only handles integers, so it ignored the .67; atof handles double values, so would have kept the 0.67. And ftemp should have been a double.)

But I can't say more as you haven't provided the full problem spec.

Andy
Last edited on
Oh, yeah, sorry about that. here it is below. Really picky like.

LAB 1: Console Programming Basics [ TheBasics.cpp ]
Following the programming conventions for this course, write a C++ console program named TheBasics.cpp, to read and print two sets of inputs, with two calculated values. Be sure to do things in the order specified.
The first set is the user's age (in whole number years) and name (first and last), in that order, each on its own separate line, following its prompt. The second set is the current outside temperature (in floating point degrees F), and the user's location (city), in that order, each on its own separate line, following its prompt. For example:

Enter your age: 21
Enter your name: Joe Student
Enter the temperature outside right now (degrees F): 45
What city are you in right now? Walnut Creek
After both sets have been read in, calculate two values: (1) the user's age one year from now, and (2) the temperature in degrees C. Finally, print the 4 input values and the two calculated values in the following 2-line format, spaced and punctuated per the example:
Joe Student is 21 years old now, and will be 22 a year from now.
It's 45 degrees F in Walnut Creek -- that's 7.2 degrees C.
Here are the specifications, in addition to the universal requirements and programming conventions explained in lab 1b above:
If the user enters a non-numeric for age or temperature, the program should not fail -- non-numeric entries should default to zero.
Print the degrees F in the same precision as entered (that is, if the input is 45.61, print it as 45.61).
Print the degrees C with one digit after the decimal point, rounded to the closest tenth of a degree.
Do NOT use the manipulator "fixed".
Use ONLY int and double for numeric variables.
For text variables, use C strings, or C++ strings, or both, as you prefer.
Be sure to include your identifying information in both comment form and cout form.
Refer to your notes from lecture, that should explain how to do these.
HINTS: If you have to use if-statements in this one, you are not doing it right. Also, the "fixed" in cout << fixed; IS a manipulator -- do NOT use it. The use of "fixed" in cout.setf(ios::fixed|ios::showpoint); is NOT a manipulator -- you may use it.

In the specs he asked to use a floating point variable for the temperature, and that means also you would have needed to use atof, but you used an integer. This is not a problem he has (making sure you follow instructions), this is a sign of a good teacher.

In programming it's very important to pay extreme attention to detail. One of the main lessons I learned in my first programming course was to read the full instructions carefully, and re-read them before submitting to ensure I followed them correctly.

edit:
You also need to print ftemp with "the same precision as entered".
Last edited on
The spec doesn't seem esp. picky to me. Specs. need to be exact, so you have something to test against.

But I'm curious about the instruction not to use the "fixed" manpulator (but allowing you to use it as a flag). Why is that?

Andy
Last edited on
Topic archived. No new replies allowed.