943,522 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3493
  • C++ RSS
Sep 10th, 2003
0

Im new to this site and c++

Expand Post »
I just started my first c++ class and we recieved our first assignment.
I dont have a compiler at home and live far away from school.
I have 2 questions:
These are the program requirements:

Program 1:

Prompt the user and let them enter three integers. Store them in three variables. Print the numbers in sorted order, from smallest to largest.

Sample run: (user input underlined)

Input integer 1 : 34
Input integer 2 : 200
Input integer 3 : -14

Sorted : -14 <= 34 <= 200


This is the code:

#include <iostream>

int main ( )
{

int one, two, three, first, sec, third;

cout << "Enter 3 integers./n";
cout << "Input Integer 1 : ";
cin >> one;
cout << "/nInput Integer 2 : ";
cin >> two;
cout << "/nInput Integer 3 : ";
cin >> three;

if ( one >= two && one >= three)
one == first;
if ( two >= one && two >= three)
two == first;
if ( three >= one && three >= two)
three = first;

if (one <= two && one >= three)
one == sec;
if (one >= two && one <= three)
one == sec;
if (two <= one && two >= three)
two == sec;
if (two >= one && two <= three)
two == sec;
if (three <= two && three >= one)
three == sec;
if (three >= two && three <= one)
three == sec;

if (one <= two && one <= three)
one = third;
if (two <= one && two <= three)
two = third;
if (three <= one && three <= two)
three = third;

cout << first << ">=" << sec << ">=" << third << ".";
cout << "Have a Nice Day!";

}

Will it work? Any changes(Im sure there is another way without all those ifs.

Program 2 requirments:

Prompt the user to type an integer in the range 0 - 1000. Allow the user to input an integer (you may assume correct type of input. Whenever the integer is not in the specified range, print an error message and make the user re-enter. Once a valid input is received, compute and print out the sum of the digits of the number.

Sample run 1: (user input underlined)

Please input an integer between 0 and 1000: 1001
* Number not in 0-1000 range. Please re-enter: -1
* Number not in 0-1000 range. Please re-enter: 456

Sum of the digits is 15

Code:

#include <iostream>

int main( )
{

int;
cout << "Enter an integer between and including 0-1000./n";
cin >> num;

if (num < 0 || num > 1000)
{
cout << "Number not between 0-1000./n";
cout << "Enter an integer between and including 0-1000./n";
cin >> num;
}

num1 = num / 100
num2 = (num % 100) / 100
num3 = (num % 100) % 10

cout << “The sum of the digits is : “;
cout << num1 + num2 + num3

}

Thanks for any help you can provide, email me at rymade@yahoo.com
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rymade is offline Offline
1 posts
since Sep 2003
Sep 11th, 2003
0

Re: Im new to this site and c++

Your first program is too big.

Something like:

C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. void Swap(int *first,int *second) {
  4. *first ^= second;
  5. *second ^= first;
  6. *first ^= second;
  7. }
  8. int main(void) {
  9. int numbers[4];
  10. for (int temp = 0;temp<3;temp++) scanf("%d",&numbers[temp]);
  11. for (temp=0;temp<2;temp++) {
  12. if (numbers[temp] > numbers[temp+1] )
  13. Swap(&numbers[temp],&numbers[temp+1]);
  14. }
  15. if (numbers[0] > numbers[1]) Swap(&numbers[0],&numbers[1];
  16. return 0;
  17. }

You said that was your first assignment, so I'm not sure you know how to use functions and whatnot, but the program I made should work. Post back any problems.

As for your second:

C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int temp = -1;
  5. while (temp < 0 || temp > 1) {
  6. printf("Input number:\n");
  7. scanf("%d",&temp);
  8. }
  9. for (temp2 = 0;temp;temp/=10) temp2+=temp%10;
  10. printf("%d",temp2);
  11. return 0;
  12. }

Now then, I thought of these from my head. I didn't compile em', but post back with any errors you might have, and I can fix em'.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
Mike29936 is offline Offline
22 posts
since Sep 2003
Sep 13th, 2003
0

Re: Im new to this site and c++

heh no offense but i dont think you wan to get his teachers hopes up that he knows #include <stdio.h>
it will get all confusing later if you try to switch between #include <iostream> and #include <stdio.h>
Reputation Points: 12
Solved Threads: 0
Newbie Poster
Armeggadon is offline Offline
10 posts
since Aug 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: compilers for c++
Next Thread in C++ Forum Timeline: c++ compiling problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC