Proceed y/n ?

I am trying, I really am , but I cant sort this out. I am trying to add a function to my code, but i cant get it right. I think i am missing a function.

I just want the do you want to proceed yes or no? To work , it yes then proceed if no then return to the start of the question.. I have removed all the horrible attempts and added codes to try and get it working. This is the code.

Thanks

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>

using namespace std;

int main()

{
string line;
  ifstream myfile ("example1.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file";



cout << "Test software" << endl;
	struct tm *current;
	time_t now;

	time(&now);
	current = localtime(&now);

	printf("The current time is: %i:%i\n", current->tm_hour, current->tm_min, current->tm_sec);

    int thisisanumber;

  cout<<"Please enter reading on Tank number 1: ";
  cin>> thisisanumber;
  cin.ignore();
  cout<<"You entered: "<< thisisanumber <<"\n"
  "Do you wish to proceed?";


  cin.get();

  cout<<"Please enter reading on Tank number 2: ";
  cin>> thisisanumber;
  cin.ignore();
  cout<<"You entered: "<< thisisanumber <<"\n"
  "Do you wish to proceed?";
  cin.get();


cout<<"Please enter reading on Tank number 3: ";
  cin>> thisisanumber;
  cin.ignore();
  cout<<"You entered: "<< thisisanumber <<"\n"
  "Do you wish to proceed?";
  cin.get();
}
I suppose you're trying to do something like this : -

1
2
3
4
5
6
7
8
9
char response;
do {
cout<<"Please enter reading on Tank number 1: ";
  cin>> thisisanumber;
  cin.ignore();
  cout<<"You entered: "<< thisisanumber <<"\n"
  "Do you wish to proceed?";

cin >> response; } while (response == 'n');
Last edited on
Thank you for your reply Dash.

I think I may have added the code incorrectly , please forgive my ignorance, and I am now getting errors .

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>

using namespace std;

int main()

{
string line;
ifstream myfile ("example1.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}

else cout << "Unable to open file";



cout << "Test software" << endl;
struct tm *current;
time_t now;

time(&now);
current = localtime(&now);

printf("The current time is: %i:%i\n", current->tm_hour, current->tm_min, current->tm_sec);

int thisisanumber;

char response;
do {
cout<<"Please enter reading on Tank number 1: ";
cin>> thisisanumber;
cin.ignore();
cout<<"You entered: "<< thisisanumber <<"\n"
"Do you wish to proceed?";

cin >> response; } while (response == 'n')

char response;
do {
cout<<"Please enter reading on Tank number 1: ";
cin>> thisisanumber;
cin.ignore();
cout<<"You entered: "<< thisisanumber <<"\n"
"Do you wish to proceed?";

cin >> response; } while (response == 'n')


char response;
do {
cout<<"Please enter reading on Tank number 1: ";
cin>> thisisanumber;
cin.ignore();
cout<<"You entered: "<< thisisanumber <<"\n"
"Do you wish to proceed?";

cin >> response; } while (response == 'n')
}


errors are as follows.

tank 3.cpp||In function 'int main()':|
tank 3.cpp|49|error: expected ';' before 'char'|
tank 3.cpp|49|error: redeclaration of 'char response'|
tank 3.cpp|39|error: 'char response' previously declared here|
tank 3.cpp|60|error: expected ';' before 'char'|
tank 3.cpp|60|error: redeclaration of 'char response'|
tank 3.cpp|39|error: 'char response' previously declared here|
tank 3.cpp|69|error: expected ';' before '}' token|
||=== Build finished: 7 errors, 0 warnings ===|

The previoulsy delcared could be a case of telling the same thing over and over again?
Please use code tags. Sorry, i forgot to add a semi colon after the do-while loop. I corrected my code. You can't redeclare response variable.
You are redeclaring response. You must do it only once.

Aceix.
Try Like this

int main()
{
char x;
x='y';
while(x=='y')
{

//function body
cout<<"Do you Wanna continue(y/n)" ;
cin>>x;
}
}
Almost there guys! Thanks for the help thus far. All that I am currently having a problem with is that the yes response is basically any key other than "n" , which is not really that bad , it is just a keyboard filled will yes keys , like a mine field.


Any way I can specify that it musy be "y" ? I will post the code, if you see the problem and understand what needs to be done , please for my sanity just add show me what needs to be added and to what line or lines please....

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
65
66
67
68
69
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>

using namespace std;

int main()

{
string line;
  ifstream myfile ("example1.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }


cout << "Test software" << endl;
	struct tm *current;
	time_t now;

	time(&now);
	current = localtime(&now);

	printf("The current time is: %i:%i\n", current->tm_hour, current->tm_min, current->tm_sec);

    int thisisanumber;

  char response;
do {
cout<<"Please enter reading on Tank number A: ";
  cin>> thisisanumber;
  cin.ignore();
  cout<<"You entered: "<< thisisanumber <<"\n"
  "Do you wish to proceed?";

cin >> response; } while (response == 'n');

cin.get();

do {
cout<<"Please enter reading on Tank number B: ";
  cin>> thisisanumber;
  cin.ignore();
  cout<<"You entered: "<< thisisanumber <<"\n"
  "Do you wish to proceed?";

cin >> response; } while (response == 'n');


cin.get();

do {
cout<<"Please enter reading on Tank number C: ";
  cin>> thisisanumber;
  cin.ignore();
  cout<<"You entered: "<< thisisanumber <<"\n"
  "Do you wish to proceed?";

cin >> response; } while (response == 'n');

}



I am sure something like the code below may help but I am not sure if anything else needs to be added before. and where..
cin >> response; } while (response =='y');
Last edited on
Topic archived. No new replies allowed.